class-wc-widget-cart.php
7 years ago
class-wc-widget-layered-nav-filters.php
7 years ago
class-wc-widget-layered-nav.php
7 years ago
class-wc-widget-price-filter.php
6 years ago
class-wc-widget-product-categories.php
7 years ago
class-wc-widget-product-search.php
8 years ago
class-wc-widget-product-tag-cloud.php
8 years ago
class-wc-widget-products.php
7 years ago
class-wc-widget-rating-filter.php
7 years ago
class-wc-widget-recent-reviews.php
8 years ago
class-wc-widget-recently-viewed.php
8 years ago
class-wc-widget-top-rated-products.php
8 years ago
class-wc-widget-products.php
215 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', '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 | 'date' => __( 'Date', 'woocommerce' ), |
| 54 | 'price' => __( 'Price', 'woocommerce' ), |
| 55 | 'rand' => __( 'Random', 'woocommerce' ), |
| 56 | 'sales' => __( 'Sales', 'woocommerce' ), |
| 57 | ), |
| 58 | ), |
| 59 | 'order' => array( |
| 60 | 'type' => 'select', |
| 61 | 'std' => 'desc', |
| 62 | 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ), |
| 63 | 'options' => array( |
| 64 | 'asc' => __( 'ASC', 'woocommerce' ), |
| 65 | 'desc' => __( 'DESC', 'woocommerce' ), |
| 66 | ), |
| 67 | ), |
| 68 | 'hide_free' => array( |
| 69 | 'type' => 'checkbox', |
| 70 | 'std' => 0, |
| 71 | 'label' => __( 'Hide free products', 'woocommerce' ), |
| 72 | ), |
| 73 | 'show_hidden' => array( |
| 74 | 'type' => 'checkbox', |
| 75 | 'std' => 0, |
| 76 | 'label' => __( 'Show hidden products', 'woocommerce' ), |
| 77 | ), |
| 78 | ); |
| 79 | |
| 80 | parent::__construct(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Query the products and return them. |
| 85 | * |
| 86 | * @param array $args Arguments. |
| 87 | * @param array $instance Widget instance. |
| 88 | * |
| 89 | * @return WP_Query |
| 90 | */ |
| 91 | public function get_products( $args, $instance ) { |
| 92 | $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; |
| 93 | $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; |
| 94 | $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; |
| 95 | $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; |
| 96 | $product_visibility_term_ids = wc_get_product_visibility_term_ids(); |
| 97 | |
| 98 | $query_args = array( |
| 99 | 'posts_per_page' => $number, |
| 100 | 'post_status' => 'publish', |
| 101 | 'post_type' => 'product', |
| 102 | 'no_found_rows' => 1, |
| 103 | 'order' => $order, |
| 104 | 'meta_query' => array(), |
| 105 | 'tax_query' => array( |
| 106 | 'relation' => 'AND', |
| 107 | ), |
| 108 | ); // WPCS: slow query ok. |
| 109 | |
| 110 | if ( empty( $instance['show_hidden'] ) ) { |
| 111 | $query_args['tax_query'][] = array( |
| 112 | 'taxonomy' => 'product_visibility', |
| 113 | 'field' => 'term_taxonomy_id', |
| 114 | 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'], |
| 115 | 'operator' => 'NOT IN', |
| 116 | ); |
| 117 | $query_args['post_parent'] = 0; |
| 118 | } |
| 119 | |
| 120 | if ( ! empty( $instance['hide_free'] ) ) { |
| 121 | $query_args['meta_query'][] = array( |
| 122 | 'key' => '_price', |
| 123 | 'value' => 0, |
| 124 | 'compare' => '>', |
| 125 | 'type' => 'DECIMAL', |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 130 | $query_args['tax_query'][] = array( |
| 131 | array( |
| 132 | 'taxonomy' => 'product_visibility', |
| 133 | 'field' => 'term_taxonomy_id', |
| 134 | 'terms' => $product_visibility_term_ids['outofstock'], |
| 135 | 'operator' => 'NOT IN', |
| 136 | ), |
| 137 | ); // WPCS: slow query ok. |
| 138 | } |
| 139 | |
| 140 | switch ( $show ) { |
| 141 | case 'featured': |
| 142 | $query_args['tax_query'][] = array( |
| 143 | 'taxonomy' => 'product_visibility', |
| 144 | 'field' => 'term_taxonomy_id', |
| 145 | 'terms' => $product_visibility_term_ids['featured'], |
| 146 | ); |
| 147 | break; |
| 148 | case 'onsale': |
| 149 | $product_ids_on_sale = wc_get_product_ids_on_sale(); |
| 150 | $product_ids_on_sale[] = 0; |
| 151 | $query_args['post__in'] = $product_ids_on_sale; |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | switch ( $orderby ) { |
| 156 | case 'price': |
| 157 | $query_args['meta_key'] = '_price'; // WPCS: slow query ok. |
| 158 | $query_args['orderby'] = 'meta_value_num'; |
| 159 | break; |
| 160 | case 'rand': |
| 161 | $query_args['orderby'] = 'rand'; |
| 162 | break; |
| 163 | case 'sales': |
| 164 | $query_args['meta_key'] = 'total_sales'; // WPCS: slow query ok. |
| 165 | $query_args['orderby'] = 'meta_value_num'; |
| 166 | break; |
| 167 | default: |
| 168 | $query_args['orderby'] = 'date'; |
| 169 | } |
| 170 | |
| 171 | return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Output widget. |
| 176 | * |
| 177 | * @param array $args Arguments. |
| 178 | * @param array $instance Widget instance. |
| 179 | * |
| 180 | * @see WP_Widget |
| 181 | */ |
| 182 | public function widget( $args, $instance ) { |
| 183 | if ( $this->get_cached_widget( $args ) ) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | ob_start(); |
| 188 | |
| 189 | $products = $this->get_products( $args, $instance ); |
| 190 | if ( $products && $products->have_posts() ) { |
| 191 | $this->widget_start( $args, $instance ); |
| 192 | |
| 193 | echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) ); |
| 194 | |
| 195 | $template_args = array( |
| 196 | 'widget_id' => $args['widget_id'], |
| 197 | 'show_rating' => true, |
| 198 | ); |
| 199 | |
| 200 | while ( $products->have_posts() ) { |
| 201 | $products->the_post(); |
| 202 | wc_get_template( 'content-widget-product.php', $template_args ); |
| 203 | } |
| 204 | |
| 205 | echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) ); |
| 206 | |
| 207 | $this->widget_end( $args ); |
| 208 | } |
| 209 | |
| 210 | wp_reset_postdata(); |
| 211 | |
| 212 | echo $this->cache_widget( $args, ob_get_clean() ); // WPCS: XSS ok. |
| 213 | } |
| 214 | } |
| 215 |