How to redirect to another page on clicking on the ‘Add To Cart’ button

You may want to refer your clients to another page upon clicking the "add to cart button. This may be an upsell page or to other areas of your website you believe may be of interest to your users. The following This tutorial will show you how to redirect to another page upon clicking on the "Add To Cart" button. The code used was provided by Dmitry Kiselyov, this solution works on Squarespace 7.1.

Complexity: Easy


Step 1

From your Squarespace account, simply add the following JavaScript below to Settings > Advanced > Code Injection > Footer and then save your changes.

<script>
function pcRedirectedPage() {
  let pcPageUrl = '/[Page URL]'; // The redirect page URL
  let pcAddToCartButton = document.querySelector('.sqs-add-to-cart-button');
  let pcProductVariants = document.querySelector('.product-variants');

  function pcVariantInStock() {
    return pcProductVariants.getAttribute('data-variant-in-stock');
  }

  function pcAddButtonClick() {
    if (pcProductVariants && pcVariantInStock() || !pcProductVariants) {
      setTimeout(function() {
        document.location.href = pcPageUrl;
      }, 1000);
    }
  }

  if (pcAddToCartButton) {
    pcAddToCartButton.addEventListener('click', pcAddButtonClick);
  }
}

document.addEventListener('DOMContentLoaded', pcRedirectedPage);
window.addEventListener('mercury:load', pcRedirectedPage);
</script>

Please note:

  • The '/[Page URL]' should reflect the page the user will be redirected to once the ‘‘ button is clicked, like '/view-more-product'.

 


All work in this guide is provided ?“AS IS”. Other than as provided in this agreement, this guide makes no other warranties, express or implied, and hereby disclaims all implied warranties, including any warranty of fitness for a particular purpose.

If you require professional advice, we recommend that you purchase the services of a developer.

Primitus Consultancy

We work with small and medium-sized businesses to help create a professional online presence. We provide a one shop full-service design studio in London, United Kingdom. 

https://primitusconsultancy.co.uk
Previous
Previous

How to reveal text on hover in Squarespace Fluid Engine

Next
Next

How to customize your SquareSpace Newsletter form