Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
25 posts Page 1 of 3
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. ;)

recent.png
recent.png (7.68 KiB) Viewed 11195 times

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.
recent-full.zip
(780 Bytes) Downloaded 717 times
Solid work! Will look into this!
Hi,

Could this be used as a wishlist ? I mean visitor clicks a button "add to wishlist" and the page will be added to the cookie. "show wishlist" button will load a page with those pages listed.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
We certainly could create a Wish list functionality with Couch. The code above would need to be modified however to accommodate the user-action of adding/removing pages. I would probably use POST <form>s for this. Let me know if you need help coding up the Wishlist.
Hi cheesypoof,

Thanks, I will give it a shot tomorrow.. I was thinking to use a cookie for this (but maybe I'm looking at the wrong function) and place the result as on a search page.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
I would definitely use a cookie, as in the code above where we create a comma-separated list of page ids.
Hi,

Placing the comma-sep. list in the cookie is something i don't seem to tackle. I have a button "add to wishlist" that places the page ID to the cookie "wishlist" I would like know how to add a next page id to the existing cookie value. ( and later on to remove it).

<cms:set all_page_ids = "<cms:get_cookie name='Wishlist'/>" 'global'/>

<!-- something like a list off all_page_ids + page_ids = new version of wishlist cookie -->
removed here my none working attempts

<cms:form action='' method="post" anchor='0'>
<cms:if k_success>
<cms:set page_ids = k_page_id 'global'/>
</cms:if>

<cms:input class="success small button" type="submit" name="wishlist" value="Add to Wishlist" />

</cms:form>


<a href="#" class="disabled small button" >Show Wishlist</a>
</div>

<!-- should be adding to an array -->
<cms:if page_ids != all_page_ids>
<cms:set_cookie name='Wishlist' value=page_ids expire='0'/>
</cms:if>
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
OK I could find a way to place a list to the cookie but can't get a solution for checking if the item already exist in the list and stopping it for adding the same value.

<cms:if k_success && k_page_id !=all_page_ids> only works for the first value added.

<cms:set all_page_ids = "<cms:get_cookie name='Wishlist'/>" 'global'/>



<cms:form action='' method="post" anchor='0'>
<cms:if k_success && k_page_id !=all_page_ids>
<cms:set page_ids = k_page_id 'global'/>
</cms:if>

<cms:input class="success small button" type="submit" name="wishlist" value="Add to Wishlist" />
<a href="<cms:add_querystring link=k_template_link k_is_list='1' querystring=all_page_ids/>" class="disabled small button" >Show Wishlist</a>

</cms:form>
</div>

<!-- array -->

<cms:capture into='ids' scope='global'>
<cms:each var=all_page_ids as='id' sep=','>
<cms:show id/>
</cms:each>
<cms:each var=page_ids as='id' sep=','>
<cms:show id/>
</cms:each>
</cms:capture>




<cms:if ids != all_page_ids>
<cms:set_cookie name='Wishlist' value=ids expire='0'/>
</cms:if>

Also i was thinking to easy about passing the wishlist to a querystring... i think
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
OK @Tomarnst, please give the code below a try. I have made the assumption that the 'Add to Wishlist' button will only be shown on the page-view and the Wishlist page is a separate template:
Code: Select all
<cms:template title='Products' clonable='1' dynamic_folders='1'/>

<cms:if k_is_page>
   <cms:no_cache/>

   <cms:set cookie_ids="<cms:get_cookie name='wishlist'/>"/>

   <cms:each var=cookie_ids as='id' sep=','>
      <cms:if id == k_page_id>
         <cms:set is_wishlist='1' scope='global'/>
      </cms:if>
   </cms:each>

   <a href="<cms:show k_page_link/>"><cms:show k_page_title/></a><br/>
   <cms:if is_wishlist>
      <a href="<cms:link 'wishlist.php'/>?remove=1&amp;id=<cms:show k_page_id/>">Remove</a>
   <cms:else/>
      <a href="<cms:link 'wishlist.php'/>?add=1&amp;id=<cms:show k_page_id/>">Add</a>
   </cms:if>
</cms:if>

Code: Select all
<cms:template title='Wishlist'/>

<cms:no_cache/>

<cms:set cookie_ids="<cms:get_cookie name='wishlist'/>"/>
<cms:set new_ids=cookie_ids scope='global'/>

<cms:set add="<cms:gpc 'add' method='get'/>"/>
<cms:set remove="<cms:gpc 'remove' method='get'/>"/>
<cms:set get_id="<cms:gpc 'id' method='get'/>" scope='global'/>

<cms:if add>
   <cms:set new_ids=get_id scope='global'/>

   <cms:each var=cookie_ids as='id' sep=','>
      <cms:if id != get_id>
         <cms:set new_ids="<cms:show new_ids/>,<cms:show id/>" scope='global'/>
      </cms:if>
   </cms:each>
</cms:if>

<cms:if remove>
   <cms:set new_ids='' scope='global'/>

   <cms:each var=cookie_ids as='id' sep=','>
      <cms:if id != get_id>
         <cms:set new_ids="<cms:if new_ids><cms:show new_ids/>,</cms:if><cms:show id/>" scope='global'/>
      </cms:if>
   </cms:each>
</cms:if>

<cms:if new_ids != cookie_ids>
   <cms:set_cookie name='wishlist' value=new_ids expire='0'/>
</cms:if>

<cms:each var=new_ids as='id' sep=','>
   <cms:pages masterpage='products.php' id=id limit='1'>
      <a href="<cms:show k_page_link/>"><cms:show k_page_title/></a><br/>
      <a href="<cms:link 'wishlist.php'/>?remove=1&amp;id=<cms:show k_page_id/>">Remove</a><br/><br/>
   </cms:pages>
</cms:each>

Let me know if this helps.
Yes, this is helps a lot.

I was trying to avoid an extra template, but your solution is working and mine attempts where not, now I can use the template for extra info. The only thing bothering in your solution is, it has to open the wishlist and redirect the visitor from the page they where on.

Many many thanks, you are THE King of the couch !
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
25 posts Page 1 of 3
cron