Site logo

Archived topic

Recently Viewed Products

4 replies · Started by Ash on June 12, 2020

Viewing posts 1–5 of 5

Trying to get recently view products to work in a hook, can't seem to get it to work.

Added shortcode, execute shortcode, after single product, location all products, but it just showed as text.

Any ideas?

Hi there,

can you share a link so i can see what is going on ?

Sorry, done

Managed to get it to show. Any ideas how I can limit it to only 4.

Using this function

add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
 
function bbloomer_recently_viewed_shortcode() {
 
   $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
   $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
 
   if ( empty( $viewed_products ) ) return;
    
   $title = '<h3>Recently Viewed Products</h3>';
   $product_ids = implode( ",", $viewed_products );
 
   return $title . do_shortcode("[products ids='$product_ids']");
   
}
This archived topic is closed to new replies.