Cookie Favourites documentation

Cookie Favourites is an ExpressionEngine addon which enables you to let your visitors store their favourite entries. The list of favourites is stored in a browser cookie, which means that the user does not have to be logged in to store favourites. Favourites can be displayed, counted, added and removed using either native EE tags or jQuery. The addon comes with an example implementation which uses both - most projects will probably use a combination of the two.

Install / Usage

The download package consists of the addon and a template group with an example implementation.

To install copy the /system/expressionengine/third_party/cookie_fav/ folder into /system/expressionengine/third_party/ on your EE install - then navigate to Addons -> Modules and hit "Intall" next to Cookie Favourites.

In the templates folder you'll find a 'cookie_fav.group'. If you're saving templates as files you can just copy that folder into your EE templates directory and you should be good to go! If not, you'll have to copy each template into EE manually.

cookie_fav.js contains jQuery code for managing the favourites list. Since ExpressionEngine is known to mess with javascript code this should not be used as a template on a real website - but rahter loaded directly from e.g. /js/cookie_fav.js ..

If you want to test-drive the example implementation simply make the above cookie_fav template group available, create a channel called 'products' (no fields etc. neccessary) - then add a couple of entries to it (only title needed). Point your browser at http://example.com/cookie_fav/ and you should see your entries and be able to favourite them.

Tags

{exp:cookie_fav:favourites}

Makes the following variables/conditionals available:

  • {has_favourites} - TRUE/FALSE
  • {total_favourites} - the total favourites count for the current user
  • {favourites_entry_ids} - a list of entry id's which can be used in the {exp:channel:entries} tag
  • Here's some example code to list the current user's favourites:

{exp:cookie_fav:favourites}
<p>You have a total of <strong>{total_favourites}</strong> favourites!</p>

{if has_favourites}
<ul>
{exp:channel:entries channel="products" limit="20" entry_id="{favourites_entry_ids}"}
<li>{title}</li>
<!-- // use other exp:channel:entries tags here to get images or whatever -->
{/exp:channel:entries}
</ul>

{/if} {!-- // has_favourites --}

{if has_favourites == FALSE}
Yo have no favourites. Please add some and come back!
{/if}
{/exp:cookie_fav:favourites}

 

{exp:cookie_fav:check entry_id=""}

Check if an entry_id is a favourite. This will enable you to use the {is_favourite} conditional like this:


{exp:channel:entries channel="products" limit="1"}

{exp:cookie_fav:check entry_id="{entry_id}"}
{if is_favourite}
This product is in your favourites list. <a href="#" class="toggle-cookie-fav removefav" id="fav-{entry_id}">Remove</a>
{if:else}
This product is not a favourite. <a href="#" class="toggle-cookie-fav addfav" id="fav-{entry_id}">Add</a>
{/if}
{/exp:cookie_fav:check}

{/exp:channel:entries}

{exp:cookie_fav:form_add entry_id=""}

This will generate a form for adding a new favourite. The entry_id parameter can be a single entry_id or a list of entry_ids (separated by |)

{exp:cookie_fav:form:remove entry_id=""}

This will generate a form for removing a favourite. The entry_id parameter can be a single entry_id or a list of entry_ids (separated by |)

{exp:cookie_fav:entry_ids}

Returns the current user's favourite entry_id's as a list that can be used with the {exp:channel:entries} tag.

Optional parameters:

  • delimeter - delimter to use to separate the entry ids, defaults to |

{exp:cookie_fav:count}

Returns the current total count of favourites.

Suggested usage

Use the {exp:cookie_fav:favourites} tag to list the favourite entries. Use the jQuery functions in cookie_fav.js to add / remove favourites. You'll need to adapt these to work with own design. If you do not want to use jQuery to add/remove favourites you can use the form_add/form_remove tags. Test-drive the example implementation that follows the bundle to get a feel of how it works.