class-wc-widget-brand-description.php
1 year ago
class-wc-widget-brand-nav.php
7 months ago
class-wc-widget-brand-thumbnails.php
1 year ago
class-wc-widget-cart.php
3 years ago
class-wc-widget-layered-nav-filters.php
2 years ago
class-wc-widget-layered-nav.php
3 months ago
class-wc-widget-price-filter.php
8 months ago
class-wc-widget-product-categories.php
7 months ago
class-wc-widget-product-search.php
5 years ago
class-wc-widget-product-tag-cloud.php
5 years ago
class-wc-widget-products.php
1 year ago
class-wc-widget-rating-filter.php
5 years ago
class-wc-widget-recent-reviews.php
2 months ago
class-wc-widget-recently-viewed.php
1 year ago
class-wc-widget-top-rated-products.php
5 years ago
class-wc-widget-recently-viewed.php
113 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Recent Products Widget. |
| 4 | * |
| 5 | * @package WooCommerce\Widgets |
| 6 | * @version 3.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | use Automattic\WooCommerce\Enums\ProductStockStatus; |
| 12 | |
| 13 | /** |
| 14 | * Widget recently viewed. |
| 15 | */ |
| 16 | class WC_Widget_Recently_Viewed extends WC_Widget { |
| 17 | |
| 18 | /** |
| 19 | * Constructor. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | $this->widget_cssclass = 'woocommerce widget_recently_viewed_products'; |
| 23 | $this->widget_description = __( "Display a list of a customer's recently viewed products.", 'woocommerce' ); |
| 24 | $this->widget_id = 'woocommerce_recently_viewed_products'; |
| 25 | $this->widget_name = __( 'Recently Viewed Products list', 'woocommerce' ); |
| 26 | $this->settings = array( |
| 27 | 'title' => array( |
| 28 | 'type' => 'text', |
| 29 | 'std' => __( 'Recently Viewed Products', 'woocommerce' ), |
| 30 | 'label' => __( 'Title', 'woocommerce' ), |
| 31 | ), |
| 32 | 'number' => array( |
| 33 | 'type' => 'number', |
| 34 | 'step' => 1, |
| 35 | 'min' => 1, |
| 36 | 'max' => 15, |
| 37 | 'std' => 10, |
| 38 | 'label' => __( 'Number of products to show', 'woocommerce' ), |
| 39 | ), |
| 40 | ); |
| 41 | |
| 42 | parent::__construct(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Output widget. |
| 47 | * |
| 48 | * @see WP_Widget |
| 49 | * @param array $args Arguments. |
| 50 | * @param array $instance Widget instance. |
| 51 | */ |
| 52 | public function widget( $args, $instance ) { |
| 53 | $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array(); // @codingStandardsIgnoreLine |
| 54 | $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) ); |
| 55 | |
| 56 | if ( empty( $viewed_products ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | ob_start(); |
| 61 | |
| 62 | $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; |
| 63 | |
| 64 | $query_args = array( |
| 65 | 'posts_per_page' => $number, |
| 66 | 'no_found_rows' => 1, |
| 67 | 'post_status' => 'publish', |
| 68 | 'post_type' => 'product', |
| 69 | 'post__in' => $viewed_products, |
| 70 | 'orderby' => 'post__in', |
| 71 | ); |
| 72 | |
| 73 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 74 | $query_args['tax_query'] = array( |
| 75 | array( |
| 76 | 'taxonomy' => 'product_visibility', |
| 77 | 'field' => 'name', |
| 78 | 'terms' => ProductStockStatus::OUT_OF_STOCK, |
| 79 | 'operator' => 'NOT IN', |
| 80 | ), |
| 81 | ); // WPCS: slow query ok. |
| 82 | } |
| 83 | |
| 84 | $r = new WP_Query( apply_filters( 'woocommerce_recently_viewed_products_widget_query_args', $query_args ) ); |
| 85 | |
| 86 | if ( $r->have_posts() ) { |
| 87 | |
| 88 | $this->widget_start( $args, $instance ); |
| 89 | |
| 90 | echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) ); |
| 91 | |
| 92 | $template_args = array( |
| 93 | 'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id, |
| 94 | ); |
| 95 | |
| 96 | while ( $r->have_posts() ) { |
| 97 | $r->the_post(); |
| 98 | wc_get_template( 'content-widget-product.php', $template_args ); |
| 99 | } |
| 100 | |
| 101 | echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) ); |
| 102 | |
| 103 | $this->widget_end( $args ); |
| 104 | } |
| 105 | |
| 106 | wp_reset_postdata(); |
| 107 | |
| 108 | $content = ob_get_clean(); |
| 109 | |
| 110 | echo $content; // WPCS: XSS ok. |
| 111 | } |
| 112 | } |
| 113 |