Many e-commerce websites these days display some variation of a 'Recently Viewed Products' list to help their customers navigate to previously visited products. I am currently working on a project that requires this feature so I'll go ahead and detail a simplified example of my solution. Let me know if you have any questions.
The code that follows is intended for the page-view. It however can be quite easily modified for use on list-views. To create the page history we need to disable caching on any view where it is displayed or updated. If we don't do this, the list will not be accurate and no cookie will be set. For demonstration purposes I have set the code to display the last 4 pages; please see the 'if' condition (id_count lt '4') to change this. Additionally, the cookie in this example has been set to expire at the end of the session (when the browser closes). Please change the expire='0' setting in the 'set_cookie' tag if you would like the page history to persist for a (longer) specific length of time (in seconds). Also, the current page has been purposefully removed from the list as there is little value in displaying it.
For an example with the privacy controls (as seen in the screenshot - clear/disable/enable) added, download the attachment below.
The code that follows is intended for the page-view. It however can be quite easily modified for use on list-views. To create the page history we need to disable caching on any view where it is displayed or updated. If we don't do this, the list will not be accurate and no cookie will be set. For demonstration purposes I have set the code to display the last 4 pages; please see the 'if' condition (id_count lt '4') to change this. Additionally, the cookie in this example has been set to expire at the end of the session (when the browser closes). Please change the expire='0' setting in the 'set_cookie' tag if you would like the page history to persist for a (longer) specific length of time (in seconds). Also, the current page has been purposefully removed from the list as there is little value in displaying it.
- Code: Select all
<?php require_once( 'couch/cms.php' ); ?>
<cms:template clonable='1'/>
<cms:if k_is_page>
<cms:no_cache/>
<cms:set old_ids = "<cms:get_cookie name='recent'/>"/>
<cms:set new_ids = k_page_id/>
<cms:if old_ids && old_ids != k_page_id>
<h4>Recently Viewed Pages</h4>
<ol>
<cms:set id_count = '0'/>
<cms:each var=old_ids as='id' sep=','>
<cms:if id != k_page_id && id_count lt '4'>
<cms:set id_count = "<cms:add id_count '1'/>" scope='parent'/>
<cms:set new_ids = "<cms:show new_ids/>,<cms:show id/>" scope='parent'/>
<cms:pages id=id limit='1'>
<li><a href="<cms:show k_page_link/>"><cms:show k_page_title/></a></li>
</cms:pages>
</cms:if>
</cms:each>
</ol>
</cms:if>
<cms:if new_ids != old_ids>
<cms:set_cookie name='recent' value=new_ids expire='0'/>
</cms:if>
</cms:if>
<?php COUCH::invoke(); ?>
For an example with the privacy controls (as seen in the screenshot - clear/disable/enable) added, download the attachment below.