2011-08-27 11:53:46 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Top Rated Products Widget
|
|
|
|
*
|
|
|
|
* Gets and displays top rated products in an unordered list
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2014-08-01 10:46:43 +00:00
|
|
|
* @author WooThemes
|
2012-08-14 17:37:50 +00:00
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
2013-05-24 15:51:58 +00:00
|
|
|
* @version 2.1.0
|
|
|
|
* @extends WC_Widget
|
2011-08-27 11:53:46 +00:00
|
|
|
*/
|
2012-10-15 10:57:58 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
|
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Top_Rated_Products extends WC_Widget {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
|
|
|
/**
|
2013-05-24 15:51:58 +00:00
|
|
|
* Constructor
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
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();
|
2011-08-27 11:53:46 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
|
|
|
* @access public
|
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
* @return void
|
|
|
|
*/
|
2013-05-24 15:51:58 +00:00
|
|
|
public function widget($args, $instance) {
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
if ( $this->get_cached_widget( $args ) )
|
2011-08-27 11:53:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
ob_start();
|
2013-05-24 15:51:58 +00:00
|
|
|
extract( $args );
|
2011-08-27 11:53:46 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
|
|
|
|
$number = absint( $instance['number'] );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
add_filter( 'posts_clauses', array( WC()->query, 'order_by_rating_post_clauses' ) );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-03-14 11:41:25 +00:00
|
|
|
$query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product' );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
$query_args['meta_query'] = WC()->query->get_meta_query();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-13 14:12:12 +00:00
|
|
|
$r = new WP_Query( $query_args );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-13 14:12:12 +00:00
|
|
|
if ( $r->have_posts() ) {
|
2011-08-27 11:53:46 +00:00
|
|
|
|
|
|
|
echo $before_widget;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-13 14:12:12 +00:00
|
|
|
if ( $title )
|
|
|
|
echo $before_title . $title . $after_title;
|
|
|
|
|
|
|
|
echo '<ul class="product_list_widget">';
|
|
|
|
|
|
|
|
while ( $r->have_posts() ) {
|
|
|
|
$r->the_post();
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( 'content-widget-product.php', array( 'show_rating' => true ) );
|
2013-05-13 14:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '</ul>';
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
echo $after_widget;
|
2013-05-13 14:12:12 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
remove_filter( 'posts_clauses', array( WC()->query, 'order_by_rating_post_clauses' ) );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
wp_reset_postdata();
|
2012-05-13 21:39:21 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$content = ob_get_clean();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-05-13 21:39:21 +00:00
|
|
|
echo $content;
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->cache_widget( $args, $content );
|
2011-08-27 11:53:46 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|