WC_Widget_Recently_Viewed

Recent Products Widget.

Defined (1)

The class is defined in the following location(s).

/includes/widgets/class-wc-widget-recently-viewed.php  
  1. class WC_Widget_Recently_Viewed extends WC_Widget { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. $this->widget_cssclass = 'woocommerce widget_recently_viewed_products'; 
  8. $this->widget_description = __( 'Display a list of recently viewed products.', 'woocommerce' ); 
  9. $this->widget_id = 'woocommerce_recently_viewed_products'; 
  10. $this->widget_name = __( 'WooCommerce recently viewed', 'woocommerce' ); 
  11. $this->settings = array( 
  12. 'title' => array( 
  13. 'type' => 'text',  
  14. 'std' => __( 'Recently viewed products', 'woocommerce' ),  
  15. 'label' => __( 'Title', 'woocommerce' ),  
  16. ),  
  17. 'number' => array( 
  18. 'type' => 'number',  
  19. 'step' => 1,  
  20. 'min' => 1,  
  21. 'max' => '',  
  22. 'std' => 10,  
  23. 'label' => __( 'Number of products to show', 'woocommerce' ),  
  24. ),  
  25. ); 
  26.  
  27. parent::__construct(); 
  28.  
  29. /** 
  30. * Output widget. 
  31. * @see WP_Widget 
  32. * @param array $args 
  33. * @param array $instance 
  34. */ 
  35. public function widget( $args, $instance ) { 
  36.  
  37. $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array(); 
  38. $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) ); 
  39.  
  40. if ( empty( $viewed_products ) ) { 
  41. return; 
  42.  
  43. ob_start(); 
  44.  
  45. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; 
  46.  
  47. $query_args = array( 
  48. 'posts_per_page' => $number,  
  49. 'no_found_rows' => 1,  
  50. 'post_status' => 'publish',  
  51. 'post_type' => 'product',  
  52. 'post__in' => $viewed_products,  
  53. 'orderby' => 'post__in',  
  54. ); 
  55.  
  56. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  57. $query_args['tax_query'] = array( 
  58. array( 
  59. 'taxonomy' => 'product_visibility',  
  60. 'field' => 'name',  
  61. 'terms' => 'outofstock',  
  62. 'operator' => 'NOT IN',  
  63. ),  
  64. ); 
  65.  
  66. $r = new WP_Query( $query_args ); 
  67.  
  68. if ( $r->have_posts() ) { 
  69.  
  70. $this->widget_start( $args, $instance ); 
  71.  
  72. echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ); 
  73.  
  74. while ( $r->have_posts() ) { 
  75. $r->the_post(); 
  76. wc_get_template( 'content-widget-product.php' ); 
  77.  
  78. echo apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ); 
  79.  
  80. $this->widget_end( $args ); 
  81.  
  82. wp_reset_postdata(); 
  83.  
  84. $content = ob_get_clean(); 
  85.  
  86. echo $content;