WooCommerce Recently Viewed Products Shortcode Plugin

A variety of days previously, WooThemes group launched mannequin 2 of the highly regarded WooCommerce plugin that allows every single WordPress site to advertise any type of merchandise very merely. Even when i’m further used to work with the superior Simple Digital Downloads plugin, by the very gifted Pippin Williamson, i wanted to go a bit deeper into WooCommerce and current you methods it’s essential to use present choices to create new capabilities. And as we communicate i’d wish to make clear you learn to create a shortcode that reveals these days seen merchandise. These days seen merchandise is an especially extremely efficient perform simply because it’s, for me, form of very basic artificial intelligence. It permits prospects to easily return to merchandise they already seen in solely a matter of seconds. you could place it in every single place in your site on account of  using a shortcode to point out these days seen merchandise is good.

Often when i create a tutorial on WPSaviour i make clear step-by-step the technique, nonetheless as as we communicate’s tuto is a bit longer, i favor to make clear the whole course of after which to give you the entire code with suggestions straight into the code.

Doing it inside a shortcode

So, we’re going to create a plugin which will register a [woocommerce_recently_viewed_products per_page=”5″] shortcode. Why making a plugin? On account of it’s the most effective strategy to retailer a perform that it’s essential to use with any theme. Once you resolve to register a shortcode proper right into a theme, the shortcode shall be obtainable offered that the theme is activated. With WooCommerce Merchandise Shortcode Plugin, whatever the theme you’re using, the perform will nonetheless be obtainable. One different essential issue, is that it’s essential to not at all ever modify WooCommerce recordsdata.

Would you like cookies? I really do!

By default WooCommerce creates a cookie that retailers important details about what a buyer does and sees on the shop. And that’s exactly the form of data we’ve to create WooCommerce Merchandise Shortcode Plugin . A really highly effective data we would like are saved proper right into a cookies often known as $_COOKIE[‘woocommerce_recently_viewed’]. Principally this cookie retailers the ID of the lastest seen merchandise. As WooCommerce is already saving these IDs.Our job is to create a fantastic query using the “put up in” query attribute.  And along with be sure that the merchandise we’ve to point out are nonetheless in stock. To take motion, we’ve to make use of the $woocommerce->query->stock_status_meta_query() methodology into the “meta_query” query attribute.

The plugin full code

i added the suggestions straight into the code, as a result of the code is pretty straightforward , and that i didn’t do a step-by-step tutorial, however when one factor isn’t clear please write a comment and that i’ll be greater than happy to make clear you each part of the code!

Plugin Title: WooCommerce - These days Thought of Merchandise
Plugin URL:
Description: Supplies a "these days seen merchandise" shortcode
Mannequin: 1.0
Creator: Remi Corson
Creator URI:
Contributors: corsonr
Textual content material Space: rc_wc_rvp
Space Path: languages

 * Register the [woocommerce_recently_viewed_products per_page="5"] shortcode
 * This shortcode reveals these days seen merchandise using WooCommerce default cookie
 * It solely has one parameter "per_page" to determine on number of devices to level out
 * @entry      public
 * @since       1.0 
 * @return      $content material materials
function rc_woocommerce_recently_viewed_products( $atts, $content material materials = null ) {

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

	// Get WooCommerce Worldwide
	worldwide $woocommerce;

	// Get these days seen 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, surrender
	if ( empty( $viewed_products ) )
		return __( 'You have not seen any product however!', 'rc_wc_rvp' );

	// Create the merchandise

	// Get merchandise per net web page
	if( !isset( $per_page ) ? $amount = 5 : $amount = $per_page )

	// Create query arguments array
    $query_args = array(
    				'posts_per_page' => $amount, 
    				'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();

    // Take a look at merchandise stock standing
    $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();

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

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

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

		// Start the loop
		whereas ( $r->have_posts()) {
			worldwide $product;

			$content material materials .= '<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 material materials .= '</ul>';


	// Get clear object
	$content material materials .= ob_get_clean();
	// Return complete content material materials
	return $content material materials;

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

Download The WPSaviour App Now

Pay in your preferred currency
Indian rupee