Skip to content

Instantly share code, notes, and snippets.

@thenexus00
Created October 19, 2014 22:39
Show Gist options
  • Save thenexus00/3f69b496d4e22c21f250 to your computer and use it in GitHub Desktop.
Save thenexus00/3f69b496d4e22c21f250 to your computer and use it in GitHub Desktop.
Recently Viewed Products in BC
<div class="recent-product" title="{{name}}">
{tag_name_nolink}
<span>
<img src='{{smallImage}}?Action=thumbnail&amp;Width=100' /><br/>
<a href="{tag_itemurl_nolink}" onclick="AddToCart({tag_catalogueid},{tag_productid},'',4,'','',true);return false;" class="btn btn-primary">Buy</a>
<a href="{tag_itemurl_nolink}" class="btn btn-default" title="view">View</a>
<br/><br/>
</span>
</div>
/* This script uses jQuery compatability mode so you may want to replace $j with $ */
function saveToCookie(){
var productID = $j('#detail-product').data('prodid'), isAllreadySaved = false;
if( !$j.cookie('visprods') ){
$j.cookie('visprods',productID,{ path: '/', domain: 'nuzest-pretty.basegraphics.com.au' , secure: false });
}
if( $j.cookie('visprods')){
var cookieGrab = $j.cookie('visprods'), productArray = null;
cookieGrab = cookieGrab.toString();
cookieGrab = cookieGrab.split(',');
productArray = cookieGrab;
for(var i=0; i<cookieGrab.length; i++){
if(cookieGrab[i] == productID){
isAllreadySaved = true;
break;
}
}
// If item is not in the cookie then update the array and replace the cookie with the new set of items.
if( !isAllreadySaved ){
for(var i=0;i<productArray.length;i++){
// Just looping through the array to get a count.
}
if ( i == 5 ) {
// shift command. If there is 5 items in the array then remove the first one (last product viewed) and add the latest viewed in.
productArray.pop();
productArray.unshift(productID);
}
else {
// if the limit has not been reached add the product to the end of the array.
productArray.unshift(productID);
}
productArray = productArray.toString();
console.log(productArray);
// Insert the array to the cookie.
$j.cookie('visprods',productArray,{ path: '/', domain: 'nuzest-pretty.basegraphics.com.au', secure: false });
}
}
}, // end site.shop.functions.saveToCookie
<!-- This sits in your layouts -->
{% assign visCookie = globals.cookie.visprods %}
{% assign visprods = visCookie | split:'%2C' %}
{% if visCookie %}
<div class="sidebar-block recview">
<h4>Recently Viewed <small>Products you have viewed</small></h4>
<div id="r-items">
{% for prodId in visprods %}
<!-- Tip: You actually do not need to specify a catalog ID to get a product -->
{module_product,-1,{{prodId}},,true,false template="/_System/CustomLayouts/recently-viewed-products.tpl"}
{% endfor %}
</div>
</div>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment