class-wc-widget-brand-description.php
1 year ago
class-wc-widget-brand-nav.php
1 year ago
class-wc-widget-brand-thumbnails.php
1 year ago
class-wc-widget-cart.php
3 years ago
class-wc-widget-layered-nav-filters.php
2 years ago
class-wc-widget-layered-nav.php
4 years ago
class-wc-widget-price-filter.php
5 years ago
class-wc-widget-product-categories.php
5 years ago
class-wc-widget-product-search.php
5 years ago
class-wc-widget-product-tag-cloud.php
5 years ago
class-wc-widget-products.php
3 years ago
class-wc-widget-rating-filter.php
5 years ago
class-wc-widget-recent-reviews.php
5 years ago
class-wc-widget-recently-viewed.php
4 years ago
class-wc-widget-top-rated-products.php
4 years ago
class-wc-widget-products.php
221 lines
| 1 | <?php |
| 2 | /** |
| 3 | * List products. One widget to rule them all. |
| 4 | * |
| 5 | * @package WooCommerce\Widgets |
| 6 | * @version 3.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Widget products. |
| 13 | */ |
| 14 | class WC_Widget_Products extends WC_Widget { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->widget_cssclass = 'woocommerce widget_products'; |
| 21 | $this->widget_description = __( "A list of your store's products.", 'woocommerce' ); |
| 22 | $this->widget_id = 'woocommerce_products'; |
| 23 | $this->widget_name = __( 'Products list', 'woocommerce' ); |
| 24 | $this->settings = array( |
| 25 | 'title' => array( |
| 26 | 'type' => 'text', |
| 27 | 'std' => __( 'Products', 'woocommerce' ), |
| 28 | 'label' => __( 'Title', 'woocommerce' ), |
| 29 | ), |
| 30 | 'number' => array( |
| 31 | 'type' => 'number', |
| 32 | 'step' => 1, |
| 33 | 'min' => 1, |
| 34 | 'max' => '', |
| 35 | 'std' => 5, |
| 36 | 'label' => __( 'Number of products to show', 'woocommerce' ), |
| 37 | ), |
| 38 | 'show' => array( |
| 39 | 'type' => 'select', |
| 40 | 'std' => '', |
| 41 | 'label' => __( 'Show', 'woocommerce' ), |
| 42 | 'options' => array( |
| 43 | '' => __( 'All products', 'woocommerce' ), |
| 44 | 'featured' => __( 'Featured products', 'woocommerce' ), |
| 45 | 'onsale' => __( 'On-sale products', 'woocommerce' ), |
| 46 | ), |
| 47 | ), |
| 48 | 'orderby' => array( |
| 49 | 'type' => 'select', |
| 50 | 'std' => 'date', |
| 51 | 'label' => __( 'Order by', 'woocommerce' ), |
| 52 | 'options' => array( |
| 53 | 'menu_order' => __( 'Menu order', 'woocommerce' ), |
| 54 | 'date' => __( 'Date', 'woocommerce' ), |
| 55 | 'price' => __( 'Price', 'woocommerce' ), |
| 56 | 'rand' => __( 'Random', 'woocommerce' ), |
| 57 | 'sales' => __( 'Sales', 'woocommerce' ), |
| 58 | ), |
| 59 | ), |
| 60 | 'order' => array( |
| 61 | 'type' => 'select', |
| 62 | 'std' => 'desc', |
| 63 | 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ), |
| 64 | 'options' => array( |
| 65 | 'asc' => __( 'ASC', 'woocommerce' ), |
| 66 | 'desc' => __( 'DESC', 'woocommerce' ), |
| 67 | ), |
| 68 | ), |
| 69 | 'hide_free' => array( |
| 70 | 'type' => 'checkbox', |
| 71 | 'std' => 0, |
| 72 | 'label' => __( 'Hide free products', 'woocommerce' ), |
| 73 | ), |
| 74 | 'show_hidden' => array( |
| 75 | 'type' => 'checkbox', |
| 76 | 'std' => 0, |
| 77 | 'label' => __( 'Show hidden products', 'woocommerce' ), |
| 78 | ), |
| 79 | ); |
| 80 | |
| 81 | parent::__construct(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Query the products and return them. |
| 86 | * |
| 87 | * @param array $args Arguments. |
| 88 | * @param array $instance Widget instance. |
| 89 | * |
| 90 | * @return WP_Query |
| 91 | */ |
| 92 | public function get_products( $args, $instance ) { |
| 93 | $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; |
| 94 | $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; |
| 95 | $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; |
| 96 | $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; |
| 97 | $product_visibility_term_ids = wc_get_product_visibility_term_ids(); |
| 98 | |
| 99 | $query_args = array( |
| 100 | 'posts_per_page' => $number, |
| 101 | 'post_status' => 'publish', |
| 102 | 'post_type' => 'product', |
| 103 | 'no_found_rows' => 1, |
| 104 | 'order' => $order, |
| 105 | 'meta_query' => array(), |
| 106 | 'tax_query' => array( |
| 107 | 'relation' => 'AND', |
| 108 | ), |
| 109 | ); // WPCS: slow query ok. |
| 110 | |
| 111 | if ( empty( $instance['show_hidden'] ) ) { |
| 112 | $query_args['tax_query'][] = array( |
| 113 | 'taxonomy' => 'product_visibility', |
| 114 | 'field' => 'term_taxonomy_id', |
| 115 | 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'], |
| 116 | 'operator' => 'NOT IN', |
| 117 | ); |
| 118 | $query_args['post_parent'] = 0; |
| 119 | } |
| 120 | |
| 121 | if ( ! empty( $instance['hide_free'] ) ) { |
| 122 | $query_args['meta_query'][] = array( |
| 123 | 'key' => '_price', |
| 124 | 'value' => 0, |
| 125 | 'compare' => '>', |
| 126 | 'type' => 'DECIMAL', |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 131 | $query_args['tax_query'][] = array( |
| 132 | array( |
| 133 | 'taxonomy' => 'product_visibility', |
| 134 | 'field' => 'term_taxonomy_id', |
| 135 | 'terms' => $product_visibility_term_ids['outofstock'], |
| 136 | 'operator' => 'NOT IN', |
| 137 | ), |
| 138 | ); // WPCS: slow query ok. |
| 139 | } |
| 140 | |
| 141 | switch ( $show ) { |
| 142 | case 'featured': |
| 143 | $query_args['tax_query'][] = array( |
| 144 | 'taxonomy' => 'product_visibility', |
| 145 | 'field' => 'term_taxonomy_id', |
| 146 | 'terms' => $product_visibility_term_ids['featured'], |
| 147 | ); |
| 148 | break; |
| 149 | case 'onsale': |
| 150 | $product_ids_on_sale = wc_get_product_ids_on_sale(); |
| 151 | $product_ids_on_sale[] = 0; |
| 152 | $query_args['post__in'] = $product_ids_on_sale; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | switch ( $orderby ) { |
| 157 | case 'menu_order': |
| 158 | $query_args['orderby'] = 'menu_order'; |
| 159 | break; |
| 160 | case 'price': |
| 161 | $query_args['meta_key'] = '_price'; // WPCS: slow query ok. |
| 162 | $query_args['orderby'] = 'meta_value_num'; |
| 163 | break; |
| 164 | case 'rand': |
| 165 | $query_args['orderby'] = 'rand'; |
| 166 | break; |
| 167 | case 'sales': |
| 168 | $query_args['meta_key'] = 'total_sales'; // WPCS: slow query ok. |
| 169 | $query_args['orderby'] = 'meta_value_num'; |
| 170 | break; |
| 171 | default: |
| 172 | $query_args['orderby'] = 'date'; |
| 173 | } |
| 174 | |
| 175 | return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Output widget. |
| 180 | * |
| 181 | * @param array $args Arguments. |
| 182 | * @param array $instance Widget instance. |
| 183 | * |
| 184 | * @see WP_Widget |
| 185 | */ |
| 186 | public function widget( $args, $instance ) { |
| 187 | if ( $this->get_cached_widget( $args ) ) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | ob_start(); |
| 192 | |
| 193 | wc_set_loop_prop( 'name', 'widget' ); |
| 194 | |
| 195 | $products = $this->get_products( $args, $instance ); |
| 196 | if ( $products && $products->have_posts() ) { |
| 197 | $this->widget_start( $args, $instance ); |
| 198 | |
| 199 | echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) ); |
| 200 | |
| 201 | $template_args = array( |
| 202 | 'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id, |
| 203 | 'show_rating' => true, |
| 204 | ); |
| 205 | |
| 206 | while ( $products->have_posts() ) { |
| 207 | $products->the_post(); |
| 208 | wc_get_template( 'content-widget-product.php', $template_args ); |
| 209 | } |
| 210 | |
| 211 | echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) ); |
| 212 | |
| 213 | $this->widget_end( $args ); |
| 214 | } |
| 215 | |
| 216 | wp_reset_postdata(); |
| 217 | |
| 218 | echo $this->cache_widget( $args, ob_get_clean() ); // WPCS: XSS ok. |
| 219 | } |
| 220 | } |
| 221 |