/includes/widgets/class-wc-widget-recently-viewed.php

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