Skip to content

hughker/magicCookies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 

Repository files navigation

magicCookies for Shopify

aka. return to collection you added product to cart from

What does it do?

If you'd like to have your Customer return from their Cart to the Collection page they last visited, this will do the trick.

Installation

Add class="continue-shopping" to relevant links in your cart.liquid Template and then add the following include at the bottom of your theme.liquid Template:

{% include 'magicCookies' %}

Here's a peek at the code inside the Snippet:

{% case template %}
  {% when 'collection' or 'product' %}
    {{ 'js.cookie.min.js' | asset_url | script_tag }}
    <script type="text/javascript">
      Cookies.set('collection', '{{ collection.handle }}');
    </script>
  {% when 'cart' %}
    {{ 'js.cookie.min.js' | asset_url | script_tag }}
    <script type="text/javascript">
      var magicCookiesCollection = Cookies.get('collection');
      if (magicCookiesCollection === undefined || magicCookiesCollection === null || magicCookiesCollection === '') {
        Cookies.set('collection', 'all');
        var magicCookiesCollection = Cookies.get('collection');
      }
      var magicCookiesTargetLinks = document.getElementsByClassName('continue-shopping');
      for ( var i in magicCookiesTargetLinks ) {
        if ( magicCookiesTargetLinks[i].className && magicCookiesTargetLinks[i].className.indexOf('continue-shopping') != -1 ) {
          magicCookiesTargetLinks[i].href = ("\/collections\/") + magicCookiesCollection;
        }
      }
    </script>
{% endcase %}

Note, you can also use CDNJS if you'd rather not host the js.cookie.min.js file along with your theme:

Replace:

{{ 'js.cookie.min.js' | asset_url | script_tag }}

With:

{{ 'https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.0.3/js.cookie.min.js' | script_tag }}

Dependencies

Contributing

Have something you want to add or remove? Please feel free to submit a pull request, I'd be more than happy to review and accept.

License

MIT. © 2015 Winston Hughes

About

An attempt to use "smart" cookies with Shopify.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages