WC_Widget_Top_Rated_Products

Top Rated Products Widget.

Defined (1)

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

/includes/widgets/class-wc-widget-top-rated-products.php  
  1. class WC_Widget_Top_Rated_Products extends WC_Widget { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->widget_cssclass = 'woocommerce widget_top_rated_products'; 
  8. $this->widget_description = __( 'Display a list of your top rated products on your site.', 'woocommerce' ); 
  9. $this->widget_id = 'woocommerce_top_rated_products'; 
  10. $this->widget_name = __( 'WooCommerce top rated products', 'woocommerce' ); 
  11. $this->settings = array( 
  12. 'title' => array( 
  13. 'type' => 'text',  
  14. 'std' => __( 'Top rated 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. ); 
  26.  
  27. parent::__construct(); 
  28.  
  29. /** 
  30. * Output widget. 
  31. * @see WP_Widget 
  32. * @param array $args 
  33. * @param array $instance 
  34. */ 
  35. public function widget( $args, $instance ) { 
  36.  
  37. if ( $this->get_cached_widget( $args ) ) { 
  38. return; 
  39.  
  40. ob_start(); 
  41.  
  42. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; 
  43.  
  44. $query_args = array( 
  45. 'posts_per_page' => $number,  
  46. 'no_found_rows' => 1,  
  47. 'post_status' => 'publish',  
  48. 'post_type' => 'product',  
  49. 'meta_key' => '_wc_average_rating',  
  50. 'orderby' => 'meta_value_num',  
  51. 'order' => 'DESC',  
  52. 'meta_query' => WC()->query->get_meta_query(),  
  53. 'tax_query' => WC()->query->get_tax_query(),  
  54. ); 
  55.  
  56. $r = new WP_Query( $query_args ); 
  57.  
  58. if ( $r->have_posts() ) { 
  59.  
  60. $this->widget_start( $args, $instance ); 
  61.  
  62. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); 
  63.  
  64. while ( $r->have_posts() ) { 
  65. $r->the_post(); 
  66. wc_get_template( 'content-widget-product.php', array( 'show_rating' => true ) ); 
  67.  
  68. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); 
  69.  
  70. $this->widget_end( $args ); 
  71.  
  72. wp_reset_postdata(); 
  73.  
  74. $content = ob_get_clean(); 
  75.  
  76. echo $content; 
  77.  
  78. $this->cache_widget( $args, $content );