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