2011-08-29 13:34:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Recent Products Widget
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2014-11-15 01:12:59 +00:00
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
|
|
|
* @version 2.3.0
|
|
|
|
* @extends WC_Widget
|
2011-08-29 13:34:36 +00:00
|
|
|
*/
|
2012-10-15 10:57:58 +00:00
|
|
|
|
2014-09-20 19:37:45 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2012-10-15 10:57:58 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
class WC_Widget_Recently_Viewed 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_recently_viewed_products';
|
|
|
|
$this->widget_description = __( 'Display a list of recently viewed products.', 'woocommerce' );
|
|
|
|
$this->widget_id = 'woocommerce_recently_viewed_products';
|
|
|
|
$this->widget_name = __( 'WooCommerce Recently Viewed', 'woocommerce' );
|
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
|
|
|
'std' => __( 'Recently Viewed Products', 'woocommerce' ),
|
|
|
|
'label' => __( 'Title', 'woocommerce' )
|
|
|
|
),
|
|
|
|
'number' => array(
|
|
|
|
'type' => 'number',
|
|
|
|
'step' => 1,
|
|
|
|
'min' => 1,
|
|
|
|
'max' => '',
|
|
|
|
'std' => 10,
|
|
|
|
'label' => __( 'Number of products to show', 'woocommerce' )
|
|
|
|
)
|
|
|
|
);
|
2014-11-15 01:12:59 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
parent::__construct();
|
2011-08-29 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 17:37:50 +00:00
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2012-08-14 17:37:50 +00:00
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
2014-11-15 01:12:59 +00:00
|
|
|
*
|
2012-08-14 17:37:50 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2014-11-15 01:12:59 +00:00
|
|
|
function widget( $args, $instance ) {
|
2011-09-06 11:11:22 +00:00
|
|
|
|
2013-01-12 13:03:38 +00:00
|
|
|
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
|
|
|
|
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
if ( empty( $viewed_products ) ) {
|
2013-01-12 13:03:38 +00:00
|
|
|
return;
|
2014-11-15 01:12:59 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2011-08-29 13:34:36 +00:00
|
|
|
ob_start();
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
|
2011-08-29 13:34:36 +00:00
|
|
|
|
2013-05-24 15:51:58 +00:00
|
|
|
$query_args = array( 'posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $viewed_products, 'orderby' => 'rand' );
|
2011-08-29 13:34:36 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['meta_query'] = array();
|
2013-11-25 14:01:32 +00:00
|
|
|
$query_args['meta_query'][] = WC()->query->stock_status_meta_query();
|
2014-11-15 01:12:59 +00:00
|
|
|
$query_args['meta_query'] = array_filter( $query_args['meta_query'] );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$r = new WP_Query( $query_args );
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2012-12-28 20:44:17 +00:00
|
|
|
if ( $r->have_posts() ) {
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_start( $args, $instance );
|
2012-12-28 20:44:17 +00:00
|
|
|
|
|
|
|
echo '<ul class="product_list_widget">';
|
2011-08-29 13:34:36 +00:00
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
while ( $r->have_posts() ) {
|
2012-12-28 20:44:17 +00:00
|
|
|
$r->the_post();
|
2013-11-25 12:45:04 +00:00
|
|
|
wc_get_template( 'content-widget-product.php' );
|
2012-12-28 20:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '</ul>';
|
|
|
|
|
2014-11-15 01:12:59 +00:00
|
|
|
$this->widget_end( $args );
|
2012-12-28 20:44:17 +00:00
|
|
|
}
|
2011-08-29 13:34:36 +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;
|
2011-08-29 13:34:36 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|