• Resolved amjulash

    (@amjulash)


    Hi, recently viewed products of woocommerce don’t work when elementor enabled on the website.

    When I disable elementor, then I can see cookies get updated with new product id of recently viewed products, but when enabled cookies don’t update with product ID and keep showing old product of when elementor was disabled.

    Can anyone please help. Thanks in advance.

    • This topic was modified 3 years, 7 months ago by amjulash.
Viewing 3 replies - 1 through 3 (of 3 total)
  • “Hi,Since you are using our Pro features on your website, you’ll need to open a support ticket at my.elementor.com.

    www.remarpro.com rules state that commercial products are not supported here.”

    Plugin Support lavig1

    (@lavig1)

    Do you still experience this issue?

    Thread Starter amjulash

    (@amjulash)

    Hello, I was able to fix it. It seems with elementor pro enabled woocommerce recent viewed cookie was not working for me. So found a solution by forcing recently viewed cookie to always.

    
    // Track product views. Always
    function wc_track_product_view_always() {
        if ( ! is_singular( 'product' ) /* xnagyg: remove this condition to run: || ! is_active_widget( false, false, 'woocommerce_recently_viewed_products', true )*/ ) {
            return;
        }
    
        global $post;
    
        if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) { // @codingStandardsIgnoreLine.
            $viewed_products = array();
        } else {
            $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ); // @codingStandardsIgnoreLine.
        }
    
        // Unset if already in viewed products list.
        $keys = array_flip( $viewed_products );
    
        if ( isset( $keys[ $post->ID ] ) ) {
            unset( $viewed_products[ $keys[ $post->ID ] ] );
        }
    
        $viewed_products[] = $post->ID;
    
        if ( count( $viewed_products ) > 15 ) {
            array_shift( $viewed_products );
        }
    
        // Store for session only.
        wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
    }
    remove_action('template_redirect', 'wc_track_product_view', 20);
    add_action( 'template_redirect', 'wc_track_product_view_always', 20 );
    

    Then set a custom posts query to show recently viewed products in elementor’s posts widget.

    
    // Elemento posts query to show recently viewed products
    add_action( 'elementor/query/customquery', function( $query ) {
    	
    	// Get WooCommerce Global
    	global $woocommerce;
    
    	// Get recently viewed product cookies data
    	$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
    	$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
    
    	// If no data, quit
    	if(empty($viewed_products)) {
    		return;
    	}
    	
    	$query->set( 'post__in', $viewed_products );
    
    });
    

    Hope to see this fixed and support on elementor.

    Thanks
    Julash

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Woocommerce recently viewed cookie problem’ is closed to new replies.