WC_Widget_Products

List products.

Defined (1)

The class is defined in the following location(s).

/includes/widgets/class-wc-widget-products.php  
  1. class WC_Widget_Products extends WC_Widget { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->widget_cssclass = 'woocommerce widget_products'; 
  8. $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' ); 
  9. $this->widget_id = 'woocommerce_products'; 
  10. $this->widget_name = __( 'WooCommerce products', 'woocommerce' ); 
  11. $this->settings = array( 
  12. 'title' => array( 
  13. 'type' => 'text',  
  14. 'std' => __( 'Products', 'woocommerce' ),  
  15. 'label' => __( 'Title', 'woocommerce' ),  
  16. ),  
  17. 'number' => array( 
  18. 'type' => 'number',  
  19. 'step' => 1,  
  20. 'min' => 1,  
  21. 'max' => '',  
  22. 'std' => 5,  
  23. 'label' => __( 'Number of products to show', 'woocommerce' ),  
  24. ),  
  25. 'show' => array( 
  26. 'type' => 'select',  
  27. 'std' => '',  
  28. 'label' => __( 'Show', 'woocommerce' ),  
  29. 'options' => array( 
  30. '' => __( 'All products', 'woocommerce' ),  
  31. 'featured' => __( 'Featured products', 'woocommerce' ),  
  32. 'onsale' => __( 'On-sale products', 'woocommerce' ),  
  33. ),  
  34. ),  
  35. 'orderby' => array( 
  36. 'type' => 'select',  
  37. 'std' => 'date',  
  38. 'label' => __( 'Order by', 'woocommerce' ),  
  39. 'options' => array( 
  40. 'date' => __( 'Date', 'woocommerce' ),  
  41. 'price' => __( 'Price', 'woocommerce' ),  
  42. 'rand' => __( 'Random', 'woocommerce' ),  
  43. 'sales' => __( 'Sales', 'woocommerce' ),  
  44. ),  
  45. ),  
  46. 'order' => array( 
  47. 'type' => 'select',  
  48. 'std' => 'desc',  
  49. 'label' => _x( 'Order', 'Sorting order', 'woocommerce' ),  
  50. 'options' => array( 
  51. 'asc' => __( 'ASC', 'woocommerce' ),  
  52. 'desc' => __( 'DESC', 'woocommerce' ),  
  53. ),  
  54. ),  
  55. 'hide_free' => array( 
  56. 'type' => 'checkbox',  
  57. 'std' => 0,  
  58. 'label' => __( 'Hide free products', 'woocommerce' ),  
  59. ),  
  60. 'show_hidden' => array( 
  61. 'type' => 'checkbox',  
  62. 'std' => 0,  
  63. 'label' => __( 'Show hidden products', 'woocommerce' ),  
  64. ),  
  65. ); 
  66.  
  67. parent::__construct(); 
  68.  
  69. /** 
  70. * Query the products and return them. 
  71. * @param array $args 
  72. * @param array $instance 
  73. * @return WP_Query 
  74. */ 
  75. public function get_products( $args, $instance ) { 
  76. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; 
  77. $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; 
  78. $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; 
  79. $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; 
  80. $product_visibility_term_ids = wc_get_product_visibility_term_ids(); 
  81.  
  82. $query_args = array( 
  83. 'posts_per_page' => $number,  
  84. 'post_status' => 'publish',  
  85. 'post_type' => 'product',  
  86. 'no_found_rows' => 1,  
  87. 'order' => $order,  
  88. 'meta_query' => array(),  
  89. 'tax_query' => array( 
  90. 'relation' => 'AND',  
  91. ),  
  92. ); 
  93.  
  94. if ( empty( $instance['show_hidden'] ) ) { 
  95. $query_args['tax_query'][] = array( 
  96. 'taxonomy' => 'product_visibility',  
  97. 'field' => 'term_taxonomy_id',  
  98. 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],  
  99. 'operator' => 'NOT IN',  
  100. ); 
  101. $query_args['post_parent'] = 0; 
  102.  
  103. if ( ! empty( $instance['hide_free'] ) ) { 
  104. $query_args['meta_query'][] = array( 
  105. 'key' => '_price',  
  106. 'value' => 0,  
  107. 'compare' => '>',  
  108. 'type' => 'DECIMAL',  
  109. ); 
  110.  
  111. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  112. $query_args['tax_query'] = array( 
  113. array( 
  114. 'taxonomy' => 'product_visibility',  
  115. 'field' => 'term_taxonomy_id',  
  116. 'terms' => $product_visibility_term_ids['outofstock'],  
  117. 'operator' => 'NOT IN',  
  118. ),  
  119. ); 
  120.  
  121. switch ( $show ) { 
  122. case 'featured' : 
  123. $query_args['tax_query'][] = array( 
  124. 'taxonomy' => 'product_visibility',  
  125. 'field' => 'term_taxonomy_id',  
  126. 'terms' => $product_visibility_term_ids['featured'],  
  127. ); 
  128. break; 
  129. case 'onsale' : 
  130. $product_ids_on_sale = wc_get_product_ids_on_sale(); 
  131. $product_ids_on_sale[] = 0; 
  132. $query_args['post__in'] = $product_ids_on_sale; 
  133. break; 
  134.  
  135. switch ( $orderby ) { 
  136. case 'price' : 
  137. $query_args['meta_key'] = '_price'; 
  138. $query_args['orderby'] = 'meta_value_num'; 
  139. break; 
  140. case 'rand' : 
  141. $query_args['orderby'] = 'rand'; 
  142. break; 
  143. case 'sales' : 
  144. $query_args['meta_key'] = 'total_sales'; 
  145. $query_args['orderby'] = 'meta_value_num'; 
  146. break; 
  147. default : 
  148. $query_args['orderby'] = 'date'; 
  149.  
  150. return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); 
  151.  
  152. /** 
  153. * Output widget. 
  154. * @see WP_Widget 
  155. * @param array $args 
  156. * @param array $instance 
  157. */ 
  158. public function widget( $args, $instance ) { 
  159. if ( $this->get_cached_widget( $args ) ) { 
  160. return; 
  161.  
  162. ob_start(); 
  163.  
  164. if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) { 
  165. $this->widget_start( $args, $instance ); 
  166.  
  167. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); 
  168.  
  169. while ( $products->have_posts() ) { 
  170. $products->the_post(); 
  171. wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) ); 
  172.  
  173. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); 
  174.  
  175. $this->widget_end( $args ); 
  176.  
  177. wp_reset_postdata(); 
  178.  
  179. echo $this->cache_widget( $args, ob_get_clean() );