WooCommerce Version 2: Creating a Shortcode to Display Recently Viewed Products
The WooThemes team recently announced the release of version 2 of the popular WooCommerce plugin. This plugin allows WordPress websites to sell any kind of products with ease. In this article, we will explore how to use existing features in WooCommerce to create new functions. Specifically, we will explain how to create a shortcode that displays recently viewed products.
Recently viewed products are a powerful feature that provides basic artificial intelligence. It allows users to easily go back to products they have already viewed in just a matter of seconds. Using a shortcode to display recently viewed products is great because it can be placed anywhere on your website.
Creating the Shortcode
We will create a plugin that registers a [woocommerce_recently_viewed_products per_page=”5″] shortcode. Creating a plugin is the easiest way to store a feature that can be used with any theme. If you register a shortcode into a theme, it will only be available if the theme is activated. With a plugin, the feature will still be available no matter which theme you are using. It is important to note that you should never modify WooCommerce files.
By default, WooCommerce creates a cookie that stores important data about what a visitor does and sees on the shop. This is the type of data we need to create our plugin. The most important data we need are stored in a cookie called $_COOKIE[‘woocommerce_recently_viewed’]. This cookie stores the ID of the latest viewed products. As WooCommerce is already saving these IDs, our job is to create the query using the “post__in” query attribute and ensure that the products we need to display are still in stock. To do this, we need to use the $woocommerce->query->stock_status_meta_query() method into the “meta_query” query attribute.
Plugin Code
The code for the plugin is simple and includes comments directly in the code. We will not provide a step-by-step tutorial, but if something is unclear, please leave a comment, and we will explain each part of the code.
“`
/*
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 = ‘
‘;
}
// 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”);
“`
Conclusion
Creating a shortcode to display recently viewed products is a great way to provide users with an easy way to go back to products they have already viewed. By using existing features in WooCommerce, we can create new functions without modifying any core files. The plugin we created registers a shortcode that can be used with any theme and provides a powerful feature that enhances the user experience.