Currently set to No Index
Currently set to No Follow

Get the Official WPSaviour App Now!


WooCommerce Recently Viewed Products Shortcode Plugin

A few days ago, WooThemes team announced version 2 of the very popular WooCommerce plugin that allows every single WordPress website to sell any kind of products very easily. Even if i’m more used to work with the awesome Easy Digital Downloads plugin, by the very talented Pippin Williamson, i wanted to go a bit deeper into WooCommerce and show you how you can use existing features to create new functions. And today i’d like to explain you how to create a shortcode that displays recently viewed products. Recently viewed products is an incredibly powerful feature simply because it’s, for me, sort of very basic artificial intelligence. It allows users to easily go back to products they already viewed in just a matter of seconds. you can place it everywhere in your website because  using a shortcode to display recently viewed products is great.

Normally when i create a tutorial on WPSaviour i explain step by step the method, but as today’s tuto is a bit longer, i prefer to explain the whole process and then to provide you the full code with comments directly into the code.

Doing it within a shortcode

So, we’re going to create a plugin that will register a [woocommerce_recently_viewed_products per_page=”5″] shortcode. Why creating a plugin? Because it’s the easiest way to store a feature that you can use with any theme. If you decide to register a shortcode into a theme, the shortcode will be available only if the theme is activated. With WooCommerce Products Shortcode Plugin, no matter the theme you’re using, the feature will still be available. Another very important thing, is that you should never ever modify WooCommerce files.

Do you like cookies? I really do!

By default WooCommerce creates a cookie that stores important data about what a visitor does and sees on the shop. And that’s exactly the type of data we need to create WooCommerce Products Shortcode Plugin . The most important data we need are stored into a cookies called $_COOKIE[‘woocommerce_recently_viewed’]. Basically this cookie stores the ID of the lastest viewed products. As WooCommerce is already saving these IDs.Our job is to create a good query using the “post in” query attribute.  And also to ensure that the products we need to display are still in stock. To do so, we need to use the $woocommerce->query->stock_status_meta_query() method into the “meta_query” query attribute.

The plugin complete code

i added the comments directly into the code, as the code is pretty simple , and i didn’t do a step by step tutorial, but if something isn’t clear please write a comment and i’ll be more than happy to explain you each part of the code!

Plugin Name: WooCommerce - Recently Viewed Products
Plugin URL:
Description: Adds a "recently viewed products" shortcode
Version: 1.0
Author: Remi Corson
Author URI:
Contributors: corsonr
Text Domain: rc_wc_rvp
Domain Path: languages

 * Register the [woocommerce_recently_viewed_products per_page="5"] shortcode
 * This shortcode displays recently viewed products using WooCommerce default cookie
 * It only has one parameter "per_page" to choose number of items to show
 * @access      public
 * @since       1.0 
 * @return      $content
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {

	// Get shortcode parameters
		"per_page" => '5'
	), $atts));

	// 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 __( 'You have not viewed any product yet!', 'rc_wc_rvp' );

	// Create the object

	// Get products per page
	if( !isset( $per_page ) ? $number = 5 : $number = $per_page )

	// Create query arguments array
    $query_args = array(
    				'posts_per_page' => $number, 
    				'no_found_rows'  => 1, 
    				'post_status'    => 'publish', 
    				'post_type'      => 'product', 
    				'post__in'       => $viewed_products, 
    				'orderby'        => 'rand'

	// Add meta_query to query args
	$query_args['meta_query'] = array();

    // Check products stock status
    $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

	// Create a new query
	$r = new WP_Query($query_args);

	// If query return results
	if ( $r->have_posts() ) {

		$content = '<ul class="rc_wc_rvp_product_list_widget">';

		// Start the loop
		while ( $r->have_posts()) {
			global $product;

			$content .= '<li>
				<a href="' . get_permalink() . '">
					' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
				</a> ' . $product->get_price_html() . '

		$content .= '</ul>';


	// Get clean object
	$content .= ob_get_clean();
	// Return whole content
	return $content;

// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products");

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogTips

Top 5 free payment gateway plugins for WordPress in India (2020)

Beginner’s GuideBlogTips

Best WooCommerce International Payment Gateways for WordPress in 2020

Best WordPress PluginsBlog

WordPress 5.4 Will Add Lazy-Loading to All Images

Beginner’s GuideBlog

How to Add a Preloader Animation to WordPress (Step by Step)

Sign up for our Newsletter and
stay informed