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. And the fact to use a shortcode to display recently viewed products is great because you can place it everywhere in your website.
Normally when I create a tutorial on WPexplorer I explain step by step the method, but as today’s tut 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 a 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 our 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 finally to create the good query using the “post__in” query attribute and 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
As the code is pretty simple I added the comments directly into the code, 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!
<?php
/*
Plugin Name: WooCommerce - Recently Viewed Products
Plugin URL: http://remicorson.com/
Description: Adds a "recently viewed products" shortcode
Version: 1.0
Author: Remi Corson
Author URI: http://remicorson.com
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
extract(shortcode_atts(array(
"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
ob_start();
// 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()) {
$r->the_post();
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() . '
</li>';
}
$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");