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

  1. <?php 
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) { 
  4. exit; 
  5.  
  6. /** 
  7. * Top Rated Products Widget. 
  8. * 
  9. * Gets and displays top rated products in an unordered list. 
  10. * 
  11. * @author WooThemes 
  12. * @category Widgets 
  13. * @package WooCommerce/Widgets 
  14. * @version 3.0.0 
  15. * @extends WC_Widget 
  16. */ 
  17. class WC_Widget_Top_Rated_Products extends WC_Widget { 
  18.  
  19. /** 
  20. * Constructor. 
  21. */ 
  22. public function __construct() { 
  23. $this->widget_cssclass = 'woocommerce widget_top_rated_products'; 
  24. $this->widget_description = __( 'Display a list of your top rated products on your site.', 'woocommerce' ); 
  25. $this->widget_id = 'woocommerce_top_rated_products'; 
  26. $this->widget_name = __( 'WooCommerce top rated products', 'woocommerce' ); 
  27. $this->settings = array( 
  28. 'title' => array( 
  29. 'type' => 'text',  
  30. 'std' => __( 'Top rated products', 'woocommerce' ),  
  31. 'label' => __( 'Title', 'woocommerce' ),  
  32. ),  
  33. 'number' => array( 
  34. 'type' => 'number',  
  35. 'step' => 1,  
  36. 'min' => 1,  
  37. 'max' => '',  
  38. 'std' => 5,  
  39. 'label' => __( 'Number of products to show', 'woocommerce' ),  
  40. ),  
  41. ); 
  42.  
  43. parent::__construct(); 
  44.  
  45. /** 
  46. * Output widget. 
  47. * 
  48. * @see WP_Widget 
  49. * 
  50. * @param array $args 
  51. * @param array $instance 
  52. */ 
  53. public function widget( $args, $instance ) { 
  54.  
  55. if ( $this->get_cached_widget( $args ) ) { 
  56. return; 
  57.  
  58. ob_start(); 
  59.  
  60. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; 
  61.  
  62. $query_args = array( 
  63. 'posts_per_page' => $number,  
  64. 'no_found_rows' => 1,  
  65. 'post_status' => 'publish',  
  66. 'post_type' => 'product',  
  67. 'meta_key' => '_wc_average_rating',  
  68. 'orderby' => 'meta_value_num',  
  69. 'order' => 'DESC',  
  70. 'meta_query' => WC()->query->get_meta_query(),  
  71. 'tax_query' => WC()->query->get_tax_query(),  
  72. ); 
  73.  
  74. $r = new WP_Query( $query_args ); 
  75.  
  76. if ( $r->have_posts() ) { 
  77.  
  78. $this->widget_start( $args, $instance ); 
  79.  
  80. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); 
  81.  
  82. while ( $r->have_posts() ) { 
  83. $r->the_post(); 
  84. wc_get_template( 'content-widget-product.php', array( 'show_rating' => true ) ); 
  85.  
  86. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); 
  87.  
  88. $this->widget_end( $args ); 
  89.  
  90. wp_reset_postdata(); 
  91.  
  92. $content = ob_get_clean(); 
  93.  
  94. echo $content; 
  95.  
  96. $this->cache_widget( $args, $content ); 
.