skip to Main Content

If I am using the following code to track recently viewed products…

/**
 * Track user woocommerce viewed products.
 */
function dorzki_wc_track_product_views() {

    if( ! is_singular( 'product' ) || is_active_widget( false, false, 'woocommerce_recently_viewed_products', true ) ) {
        return;
    }

    global $post;

    if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) {

        $viewed_products = array();

    } else {

        $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) );

    }

    $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 );

    }

    wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );

}

add_action( 'template_redirect', 'dorzki_wc_track_product_views', 20 );

and this code to display recently view products…

/**
 * Display recently viewed products.
 */
function dorzki_wc_display_products_viewed() {

    if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) ) {
        return;
    }

    $total_products = apply_filters( 'loop_shop_columns', get_option( 'woocommerce_catalog_columns', 4 ) );
    $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) );

    $products = array_slice( $viewed_products, 0, $total_products );
    $ids = implode( ',', $products );

    echo "<h2>" . esc_html__( 'Recently Viewed Products', 'dorzki' ) . "</h2>";
    echo do_shortcode( "[products ids='{$ids}']" );

}

add_action( 'woocommerce_after_cart', 'dorzki_wc_display_products_viewed' );

How can I obtain the ‘count’? Ideally I would like to create a shortcode that will output how many products the user has viewed recently.

2

Answers


  1. Chosen as BEST ANSWER

    I've tried this and I 'think' it works??

        function recently_count() {
        $total_products = apply_filters( 'loop_shop_columns', get_option( 'woocommerce_catalog_columns', 4 ) );
        $viewed_products = wp_parse_id_list( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) );
        $products = array_slice( $viewed_products, 0, $total_products );
        $ids = implode( ',', $products );
    
            if ($ids == 0) { 
                echo '0';
            }
    
            else {
                echo count($ids);
            }
    
    }
    add_shortcode( 'recently_count', 'recently_count' );
    

  2. To just count the number of recently viewed products and return that in a shortcode, then you just have to count the exploded array of the cookie value.

    Also, remember that you never echo a shortcode output, it must be returned.

    function recently_count() {
        return isset( $_COOKIE['woocommerce_recently_viewed'] ) ? count( (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) ) : 0;
    }
    add_shortcode( 'recently_count', 'recently_count' );
    

    This will return an integer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search