woocommerce/includes/widgets/class-wc-widget-top-rated-p...

104 lines
2.5 KiB
PHP

<?php
/**
* Top Rated Products Widget
*
* Gets and displays top rated products in an unordered list
*
* @author WooThemes
* @category Widgets
* @package WooCommerce/Widgets
* @version 2.1.0
* @extends WC_Widget
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Widget_Top_Rated_Products extends WC_Widget {
/**
* Constructor
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_top_rated_products';
$this->widget_description = __( 'Display a list of your top rated products on your site.', 'woocommerce' );
$this->widget_id = 'woocommerce_top_rated_products';
$this->widget_name = __( 'WooCommerce Top Rated Products', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Top Rated Products', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' )
),
'number' => array(
'type' => 'number',
'step' => 1,
'min' => 1,
'max' => '',
'std' => 5,
'label' => __( 'Number of products to show', 'woocommerce' )
)
);
parent::__construct();
}
/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
* @return void
*/
public function widget($args, $instance) {
global $woocommerce;
if ( $this->get_cached_widget( $args ) )
return;
ob_start();
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$number = absint( $instance['number'] );
add_filter( 'posts_clauses', array( $woocommerce->query, 'order_by_rating_post_clauses' ) );
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );
$query_args['meta_query'] = $woocommerce->query->get_meta_query();
$r = new WP_Query( $query_args );
if ( $r->have_posts() ) {
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo '<ul class="product_list_widget">';
while ( $r->have_posts() ) {
$r->the_post();
woocommerce_get_template( 'content-widget-product.php', array( 'show_rating' => true ) );
}
echo '</ul>';
echo $after_widget;
}
remove_filter( 'posts_clauses', array( $woocommerce->query, 'order_by_rating_post_clauses' ) );
wp_reset_postdata();
$content = ob_get_clean();
echo $content;
$this->cache_widget( $args, $content );
}
}
register_widget( 'WC_Widget_Top_Rated_Products' );