products-grid.php
172 lines
| 1 | <?php |
| 2 | function auxin_products_grid( $args= array() ) { |
| 3 | |
| 4 | global $post; |
| 5 | |
| 6 | $defaults = array ( |
| 7 | 'product_type' => '', // available values : recent, featured, top_rated, sale, deal, best_selling |
| 8 | 'title' => '', // header title (required) |
| 9 | 'subtitle' => '', // header subtitle |
| 10 | 'cat' => '', |
| 11 | 'num' => '8', // max generated entry |
| 12 | 'order_by' => 'date', |
| 13 | 'order' => 'DESC', |
| 14 | 'only_products__in' => '', // display only these post IDs. array or string comma separated |
| 15 | 'include' => '', // include these post IDs in result too. array or string comma separated |
| 16 | 'exclude' => '', // exclude these post IDs from result. array or string comma separated |
| 17 | 'offset' => '', |
| 18 | 'desktop_cnum' => 4, |
| 19 | 'post_type' => 'product', |
| 20 | 'taxonomy' => 'product_cat', // the taxonomy that we intent to display in post info |
| 21 | 'tax_args' => '', |
| 22 | 'content_width' => '', |
| 23 | 'extra_classes' => '', |
| 24 | 'terms' => ' ', |
| 25 | 'universal_id' => '', |
| 26 | 'use_wp_query' => false, // true to use the global wp_query, false to use internal custom query |
| 27 | 'reset_query' => true, |
| 28 | 'wp_query_args' => array(), // additional wp_query args |
| 29 | 'query_args' => array(), |
| 30 | 'custom_wp_query' => '', |
| 31 | 'base' => 'aux_products_grid', |
| 32 | 'base_class' => 'aux-widget-products-grid' |
| 33 | ); |
| 34 | |
| 35 | $args = wp_parse_args( $args, $defaults ); |
| 36 | |
| 37 | if( ( empty( $args['terms'] ) || $args['terms'] === "all" ) && !empty( $args['cat'] ) ) { |
| 38 | $tax_args = array( |
| 39 | array( |
| 40 | 'taxonomy' => 'product_cat', |
| 41 | 'field' => 'term_id', |
| 42 | 'terms' => $args['cat'] |
| 43 | ) |
| 44 | ); |
| 45 | } else if ( ! empty( $args['terms'] ) ) { |
| 46 | $tax_args = array( |
| 47 | array( |
| 48 | 'taxonomy' => $args['taxonomy'], |
| 49 | 'field' => 'term_id', |
| 50 | 'terms' => $args['terms'] |
| 51 | ) |
| 52 | ); |
| 53 | } else if ( empty( $args['cat'] ) ) { |
| 54 | $tax_args = array(); |
| 55 | } |
| 56 | |
| 57 | if ( empty( $args['cat'] ) && $args['taxonomy'] == 'product_tag' && $args['terms'] === "all" ) { |
| 58 | $tax_args = []; |
| 59 | } |
| 60 | |
| 61 | $query_args = array( |
| 62 | 'post_type' => 'product', |
| 63 | 'posts_per_page' => $args['num'], |
| 64 | 'orderby' => $args['order_by'], |
| 65 | 'order' => $args['order'], |
| 66 | 'tax_query' => $tax_args, |
| 67 | 'post_status' => 'publish', |
| 68 | 'include_posts__in' => $args['include'], // include posts in this list |
| 69 | 'posts__not_in' => $args['exclude'], // exclude posts in this list |
| 70 | 'posts__in' => $args['only_products__in'], // only posts in this list |
| 71 | 'exclude_without_media' => $args['exclude_without_media'] |
| 72 | ); |
| 73 | |
| 74 | if ( isset( $args['meta_key'] ) ) { |
| 75 | $query_args['meta_key'] = $args['meta_key']; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | // add the additional query args if available |
| 80 | if( $args['query_args'] ){ |
| 81 | $query_args = wp_parse_args( $query_args, $args['query_args'] ); |
| 82 | } |
| 83 | |
| 84 | if ( $args['product_type'] ) { |
| 85 | switch ( $args['product_type'] ) { |
| 86 | case 'featured': |
| 87 | $query_args['tax_query'][] = array( |
| 88 | 'taxonomy' => 'product_visibility', |
| 89 | 'field' => 'name', |
| 90 | 'terms' => 'featured', |
| 91 | ); |
| 92 | break; |
| 93 | |
| 94 | case 'best_selling': |
| 95 | $query_args['orderby'] = 'meta_value_num'; |
| 96 | break; |
| 97 | |
| 98 | case 'sale': |
| 99 | $query_args['meta_query'] = WC()->query->get_meta_query(); |
| 100 | $query_args['post__in'] = wc_get_product_ids_on_sale(); |
| 101 | break; |
| 102 | |
| 103 | case 'deal': |
| 104 | $today = time(); |
| 105 | $query_args['meta_query'] = array ( |
| 106 | 'relation' => 'AND', |
| 107 | array ( |
| 108 | 'key' => '_sale_price_dates_from', |
| 109 | 'value' => '', |
| 110 | 'compare' => '!=' |
| 111 | ), |
| 112 | array ( |
| 113 | 'key' => '_sale_price_dates_from', |
| 114 | 'value' => $today, |
| 115 | 'compare' => '<=' |
| 116 | ), |
| 117 | array ( |
| 118 | 'key' => '_sale_price_dates_to', |
| 119 | 'value' => '', |
| 120 | 'compare' => '!=' |
| 121 | ), |
| 122 | array ( |
| 123 | 'key' => '_sale_price_dates_to', |
| 124 | 'value' => $today, |
| 125 | 'compare' => '>=' |
| 126 | ), |
| 127 | ); |
| 128 | break; |
| 129 | |
| 130 | case 'top_rated': |
| 131 | $query_args['meta_query'] = WC()->query->get_meta_query(); |
| 132 | add_filter( 'posts_clauses', array( 'WC_Shortcodes', 'order_by_rating_post_clauses' ) ); |
| 133 | break; |
| 134 | default: // i.e. recent product |
| 135 | $query_args['meta_query'] = WC()->query->get_meta_query(); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | ob_start(); |
| 141 | |
| 142 | // pass the args through the auxin query parser |
| 143 | $wp_query = new WP_Query( auxin_parse_query_args( $query_args ) ); |
| 144 | |
| 145 | $have_posts = $wp_query->have_posts(); |
| 146 | |
| 147 | if( $have_posts ){ |
| 148 | |
| 149 | while ( $wp_query->have_posts() ) { |
| 150 | |
| 151 | $wp_query->the_post(); |
| 152 | |
| 153 | wc_get_template( 'content-product.php' ); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | |
| 160 | if( $args['reset_query'] ){ |
| 161 | wp_reset_postdata(); |
| 162 | } |
| 163 | |
| 164 | // return false if no result found |
| 165 | if( ! $have_posts ){ |
| 166 | ob_get_clean(); |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | return ob_get_clean(); |
| 171 | }; |
| 172 |