PluginProbe ʕ •ᴥ•ʔ
ShopPress – Shop Builder for Elementor and WooCommerce / trunk
ShopPress – Shop Builder for Elementor and WooCommerce vtrunk
shop-press / Modules / RecentlyViewedProducts.php
shop-press / Modules Last commit date
VariationSwatches 2 months ago Wishlist 2 months ago AdminMessage.php 2 months ago AjaxSearch.php 2 months ago Backorder.php 2 months ago CatalogMode.php 2 years ago Compare.php 2 months ago DefaultTemplates.php 2 years ago FlashSalesCountdown.php 1 year ago MenuCart.php 6 months ago MobilePanel.php 2 months ago MultiStep.php 1 year ago Notifications.php 2 months ago QuickView.php 2 months ago RecentlyViewedProducts.php 2 years ago RenameLabel.php 6 months ago Shopify.php 1 year ago SingleAjaxAddToCart.php 2 months ago SizeChart.php 2 months ago StickyAddToCart.php 2 months ago
RecentlyViewedProducts.php
58 lines
1 <?php
2 /**
3 * Recently viewed products.
4 *
5 * @package ShopPress
6 */
7
8 namespace ShopPress\Modules;
9
10 defined( 'ABSPATH' ) || exit;
11
12 class RecentlyViewedProducts {
13 /**
14 * Init.
15 *
16 * @since 1.2.0
17 */
18 public static function init() {
19 add_action( 'template_redirect', array( __CLASS__, 'track_product_view' ), 20 );
20 }
21
22 /**
23 * Track product views.
24 *
25 * @since 1.1.0
26 */
27 public static function track_product_view() {
28
29 if ( ! is_singular( 'product' ) ) {
30 return;
31 }
32
33 global $post;
34
35 if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // @codingStandardsIgnoreLine.
36 $viewed_products = array();
37 } else {
38 $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // @codingStandardsIgnoreLine.
39 }
40
41 // Unset if already in viewed products list.
42 $keys = array_flip( $viewed_products );
43
44 if ( isset( $keys[ $post->ID ] ) ) {
45 unset( $viewed_products[ $keys[ $post->ID ] ] );
46 }
47
48 $viewed_products[] = $post->ID;
49
50 if ( count( $viewed_products ) > 15 ) {
51 array_shift( $viewed_products );
52 }
53
54 // Store for session only.
55 wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
56 }
57 }
58