2011-08-29 13:34:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Recent Products Widget.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Widgets
|
2018-03-09 20:48:28 +00:00
|
|
|
* @version 3.3.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget recently viewed.
|
2011-08-29 13:34:36 +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
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +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';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_description = __( "Display a list of a customer's recently viewed products.", 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->widget_id = 'woocommerce_recently_viewed_products';
|
2017-08-25 11:07:17 +00:00
|
|
|
$this->widget_name = __( 'Recent Viewed Products', 'woocommerce' );
|
2013-05-24 15:51:58 +00:00
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
2017-08-25 11:07:17 +00:00
|
|
|
'std' => __( 'Recently Viewed Products', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' ),
|
2013-05-24 15:51:58 +00:00
|
|
|
),
|
|
|
|
'number' => array(
|
|
|
|
'type' => 'number',
|
|
|
|
'step' => 1,
|
|
|
|
'min' => 1,
|
2017-11-29 21:24:39 +00:00
|
|
|
'max' => 15,
|
2013-05-24 15:51:58 +00:00
|
|
|
'std' => 10,
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Number of products to show', 'woocommerce' ),
|
|
|
|
),
|
2013-05-24 15:51:58 +00:00
|
|
|
);
|
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
|
|
|
/**
|
2016-01-06 18:58:38 +00:00
|
|
|
* Output widget.
|
2012-08-14 17:37:50 +00:00
|
|
|
*
|
|
|
|
* @see WP_Widget
|
2018-03-09 20:48:28 +00:00
|
|
|
* @param array $args Arguments.
|
|
|
|
* @param array $instance Widget instance.
|
2012-08-14 17:37:50 +00:00
|
|
|
*/
|
2016-04-26 17:26:56 +00:00
|
|
|
public function widget( $args, $instance ) {
|
2018-03-09 20:48:28 +00:00
|
|
|
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array(); // @codingStandardsIgnoreLine
|
2016-08-24 22:46:23 +00:00
|
|
|
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
|
2013-01-12 13:03:38 +00:00
|
|
|
|
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
|
|
|
|
2016-12-08 10:56:45 +00:00
|
|
|
$query_args = array(
|
|
|
|
'posts_per_page' => $number,
|
|
|
|
'no_found_rows' => 1,
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'post_type' => 'product',
|
|
|
|
'post__in' => $viewed_products,
|
|
|
|
'orderby' => 'post__in',
|
|
|
|
);
|
2011-08-29 13:34:36 +00:00
|
|
|
|
2016-12-08 10:56:45 +00:00
|
|
|
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
|
|
|
|
$query_args['tax_query'] = array(
|
|
|
|
array(
|
|
|
|
'taxonomy' => 'product_visibility',
|
|
|
|
'field' => 'name',
|
|
|
|
'terms' => 'outofstock',
|
|
|
|
'operator' => 'NOT IN',
|
|
|
|
),
|
2018-03-09 20:48:28 +00:00
|
|
|
); // WPCS: slow query ok.
|
2016-12-08 10:56:45 +00:00
|
|
|
}
|
2012-08-14 17:37:50 +00:00
|
|
|
|
2017-11-15 10:18:59 +00:00
|
|
|
$r = new WP_Query( apply_filters( 'woocommerce_recently_viewed_products_widget_query_args', $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
|
|
|
|
2017-10-27 14:35:34 +00:00
|
|
|
echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
|
2011-08-29 13:34:36 +00:00
|
|
|
|
2017-10-03 20:52:36 +00:00
|
|
|
$template_args = array(
|
2019-08-01 17:43:59 +00:00
|
|
|
'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id,
|
2017-10-03 20:52: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();
|
2017-10-03 20:52:36 +00:00
|
|
|
wc_get_template( 'content-widget-product.php', $template_args );
|
2012-12-28 20:44:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 14:35:34 +00:00
|
|
|
echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
|
2012-12-28 20:44:17 +00:00
|
|
|
|
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
|
|
|
|
2018-03-09 20:48:28 +00:00
|
|
|
echo $content; // WPCS: XSS ok.
|
2011-08-29 13:34:36 +00:00
|
|
|
}
|
2013-05-24 15:51:58 +00:00
|
|
|
}
|