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