aka. return to collection you added product to cart from
- Uses JavaScript Cookie
- Sets
magicCookiesas the.noConflictnamespace (see here)
If you'd like to have your Customer return from their Cart to the Collection page they last visited, this will do the trick.
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 }}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.
MIT. © 2015 Winston Hughes