Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Last active October 14, 2016 16:26
Show Gist options
  • Select an option

  • Save sandcastle/ca3712403425f4ed94facf6722bb9d44 to your computer and use it in GitHub Desktop.

Select an option

Save sandcastle/ca3712403425f4ed94facf6722bb9d44 to your computer and use it in GitHub Desktop.
Recently viewed product tracking on woocommerce with a shortcode and cookies
/**
* https://github.com/woocommerce/woocommerce/issues/9724#issuecomment-160618200
*/
function custom_track_product_view() {
if ( ! is_singular( 'product' ) ) {
return;
}
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) )
$viewed_products = array();
else
$viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );
if ( ! in_array( $post->ID, $viewed_products ) ) {
$viewed_products[] = $post->ID;
}
if ( sizeof( $viewed_products ) > 15 ) {
array_shift( $viewed_products );
}
// Store for session only
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
add_action( 'template_redirect', 'custom_track_product_view', 20 );
/**
* 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
*
* Source: http://snippets.drumcreative.com/?p=91
*/
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 (empty($r)) {
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
}
?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php woocommerce_product_loop_end(); ?>
<?php wp_reset_postdata();
return '<div class="woocommerce columns-5 facetwp-template">' . ob_get_clean() . '</div>';
// ----
// 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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment