Adding a link that goes to the last collection viewed

Solved
TheShiz
Excursionist
13 1 3

Hello!

I found this information online: "Add a 'Continue shopping' link to your cart", specifically the last component of the tutorial for "a link that goes to the last collection viewed".

I would like to implement this, however NOT as part of a "continue shopping" button; nothing to do with my cart. I simply want to make a button or link in my Product Template that says "go back to collection" etc. 

For your convenience, the code in the linked example is as follows:

 

<a
  id="continue-shopping"
  href=""
  title="Continue shopping"
  >Continue shopping</a
>

 

JS to go into the Theme.Liquid body:

 

<script>
  if(Storage !== undefined) {

    var defaultLink = "/collections/all";

    {% if template contains 'collection' %}
      sessionStorage.collection = "{{ collection.url }}";
    {% endif %}

    {% if template contains 'cart' %}
      if( !sessionStorage.collection ) {
        sessionStorage.collection = defaultLink;
      }
      document.getElementById("continue-shopping").href = sessionStorage.collection;
    {% endif %}

    }
</script>

 

Will the above script need to be amended and if so, could someone please provide me with adjusted code to suit my needs?

Thanking you in advance.

0 Likes
FrankMoore
Explorer
43 10 9

This is an accepted solution.