/includes/widgets/class-wc-widget-products.php

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; 
  5.  
  6. /** 
  7. * List products. One widget to rule them all. 
  8. * 
  9. * @author WooThemes 
  10. * @category Widgets 
  11. * @package WooCommerce/Widgets 
  12. * @version 2.3.0 
  13. * @extends WC_Widget 
  14. */ 
  15. class WC_Widget_Products extends WC_Widget { 
  16.  
  17. /** 
  18. * Constructor. 
  19. */ 
  20. public function __construct() { 
  21. $this->widget_cssclass = 'woocommerce widget_products'; 
  22. $this->widget_description = __( 'Display a list of your products on your site.', 'woocommerce' ); 
  23. $this->widget_id = 'woocommerce_products'; 
  24. $this->widget_name = __( 'WooCommerce products', 'woocommerce' ); 
  25. $this->settings = array( 
  26. 'title' => array( 
  27. 'type' => 'text',  
  28. 'std' => __( 'Products', 'woocommerce' ),  
  29. 'label' => __( 'Title', 'woocommerce' ),  
  30. ),  
  31. 'number' => array( 
  32. 'type' => 'number',  
  33. 'step' => 1,  
  34. 'min' => 1,  
  35. 'max' => '',  
  36. 'std' => 5,  
  37. 'label' => __( 'Number of products to show', 'woocommerce' ),  
  38. ),  
  39. 'show' => array( 
  40. 'type' => 'select',  
  41. 'std' => '',  
  42. 'label' => __( 'Show', 'woocommerce' ),  
  43. 'options' => array( 
  44. '' => __( 'All products', 'woocommerce' ),  
  45. 'featured' => __( 'Featured products', 'woocommerce' ),  
  46. 'onsale' => __( 'On-sale products', 'woocommerce' ),  
  47. ),  
  48. ),  
  49. 'orderby' => array( 
  50. 'type' => 'select',  
  51. 'std' => 'date',  
  52. 'label' => __( 'Order by', 'woocommerce' ),  
  53. 'options' => array( 
  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. * Query the products and return them. 
  85. * @param array $args 
  86. * @param array $instance 
  87. * @return WP_Query 
  88. */ 
  89. public function get_products( $args, $instance ) { 
  90. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; 
  91. $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; 
  92. $orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std']; 
  93. $order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std']; 
  94. $product_visibility_term_ids = wc_get_product_visibility_term_ids(); 
  95.  
  96. $query_args = array( 
  97. 'posts_per_page' => $number,  
  98. 'post_status' => 'publish',  
  99. 'post_type' => 'product',  
  100. 'no_found_rows' => 1,  
  101. 'order' => $order,  
  102. 'meta_query' => array(),  
  103. 'tax_query' => array( 
  104. 'relation' => 'AND',  
  105. ),  
  106. ); 
  107.  
  108. if ( empty( $instance['show_hidden'] ) ) { 
  109. $query_args['tax_query'][] = array( 
  110. 'taxonomy' => 'product_visibility',  
  111. 'field' => 'term_taxonomy_id',  
  112. 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],  
  113. 'operator' => 'NOT IN',  
  114. ); 
  115. $query_args['post_parent'] = 0; 
  116.  
  117. if ( ! empty( $instance['hide_free'] ) ) { 
  118. $query_args['meta_query'][] = array( 
  119. 'key' => '_price',  
  120. 'value' => 0,  
  121. 'compare' => '>',  
  122. 'type' => 'DECIMAL',  
  123. ); 
  124.  
  125. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  126. $query_args['tax_query'] = array( 
  127. array( 
  128. 'taxonomy' => 'product_visibility',  
  129. 'field' => 'term_taxonomy_id',  
  130. 'terms' => $product_visibility_term_ids['outofstock'],  
  131. 'operator' => 'NOT IN',  
  132. ),  
  133. ); 
  134.  
  135. switch ( $show ) { 
  136. case 'featured' : 
  137. $query_args['tax_query'][] = array( 
  138. 'taxonomy' => 'product_visibility',  
  139. 'field' => 'term_taxonomy_id',  
  140. 'terms' => $product_visibility_term_ids['featured'],  
  141. ); 
  142. break; 
  143. case 'onsale' : 
  144. $product_ids_on_sale = wc_get_product_ids_on_sale(); 
  145. $product_ids_on_sale[] = 0; 
  146. $query_args['post__in'] = $product_ids_on_sale; 
  147. break; 
  148.  
  149. switch ( $orderby ) { 
  150. case 'price' : 
  151. $query_args['meta_key'] = '_price'; 
  152. $query_args['orderby'] = 'meta_value_num'; 
  153. break; 
  154. case 'rand' : 
  155. $query_args['orderby'] = 'rand'; 
  156. break; 
  157. case 'sales' : 
  158. $query_args['meta_key'] = 'total_sales'; 
  159. $query_args['orderby'] = 'meta_value_num'; 
  160. break; 
  161. default : 
  162. $query_args['orderby'] = 'date'; 
  163.  
  164. return new WP_Query( apply_filters( 'woocommerce_products_widget_query_args', $query_args ) ); 
  165.  
  166. /** 
  167. * Output widget. 
  168. * 
  169. * @see WP_Widget 
  170. * 
  171. * @param array $args 
  172. * @param array $instance 
  173. */ 
  174. public function widget( $args, $instance ) { 
  175. if ( $this->get_cached_widget( $args ) ) { 
  176. return; 
  177.  
  178. ob_start(); 
  179.  
  180. if ( ( $products = $this->get_products( $args, $instance ) ) && $products->have_posts() ) { 
  181. $this->widget_start( $args, $instance ); 
  182.  
  183. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); 
  184.  
  185. while ( $products->have_posts() ) { 
  186. $products->the_post(); 
  187. wc_get_template( 'content-widget-product.php', array( 'show_rating' => false ) ); 
  188.  
  189. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); 
  190.  
  191. $this->widget_end( $args ); 
  192.  
  193. wp_reset_postdata(); 
  194.  
  195. echo $this->cache_widget( $args, ob_get_clean() ); 
.