"CONTINUE SHOPPING” link on CART page to re-direct customer back to the last URL

New Member
3 0 0

We would like to create a "CONTINUE SHOPPING” link on the CART page, so that it can re-direct customer to the last URL (not just the collection page). 

 

We are able to add a link that goes to the last collection viewed by adding the following HTML on cart.liquid, then javascript on theme.liquid from this URL: https://help.shopify.com/en/themes/customization/cart/add-back-link

 

We tried to modify the javascript but it did not work and we could not solve our problem as we would like customer to go back to the previous page instead.  Would anyone please help us on this? Thank you very much.

 

 

<a id="continue-shopping" href="" title="Continue shopping">Continue shopping</a>
<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>

 

 

0 Likes
Highlighted
Shopify Partner
468 85 109

If you want to go previous page, you can just the following code to a tag

onclick="window.history.go(-1); return false;"

 

So the final version of the a tag will be 

 

<a id="continue-shopping" href="" title="Continue shopping" onclick="window.history.go(-1); return false;">Continue shopping</a>

 

and the script part can be removed

Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes
Highlighted
New Member
3 0 0

Thank you very much and it works! 

 

I have one more question, if i let customer go "back" to the product's page after they put an item to the cart.  Will the "already-add-to-cart" product be kept in the cart page? 

I did some tryouts and it seems to be working.  Thanks.

0 Likes
Highlighted
Shopify Partner
468 85 109

Once you added product in the cart, it will be there even if you go back.

Even if you close the tab of that site and then again enter into it, it will be there ))

Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes
Highlighted
New Member
6 0 0

Hello sarhov,

 

I want to accomplish the same thing, but it did not work.  Obviously, I'm missing something likely obvious.

 

I added the code you recommended:

<a id="continue-shopping" href="" title="Continue shopping" onclick="window.history.go(-1); return false;">Continue shopping</a>

That was added right under the 'checkout' button code:

<button type="submit" id="checkout" name="checkout" class="btn">{{ 'cart.general.checkout' | t }}</button>

I also got rid of the javascript code that I had added to the theme.liquid page.

 

What previous code that was before taking me to the 'collections' page (after clicking on the "Continue Shopping" button) should I get rid of?

Might that be the issue?

 

If I get rid of the below line of code, might that help?

  <div class="medium-up--hide">
                <a href="collections/all" class="btn cart__update cart__continue--large" >{{ 'cart.general.continue_shopping' | t }}</a>  
              </div>

 

Thanks in advance for your help.

0 Likes