Skip to content

Latest commit

 

History

History
62 lines (52 loc) · 2.25 KB

README.md

File metadata and controls

62 lines (52 loc) · 2.25 KB

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