Jump to content

Creating Delay for JS Redirect with Add to Cart Button

Recommended Posts

Hello!

I'm trying to create a javascript function where after a customer adds something to cart, they are sent to the cart. At the moment, they stay on the product page.

I created this javascript, and it works to send the customer to the cart. However, the product doesn't appear in the cart right away. It seems like I need a delay to give Squarespace some time to add the product to the cart. I'm hoping to add a 2 or 3 second delay to allow for this. The button says "Adding..." then "Added!", so I'm not concerned about experience. 

Any help that can be provided would be great!

<script>
$(function() {
  var $button = $('.sqs-add-to-cart-button');
  $button.on('click', function(){
 window.location.href = 'https://www.pamelacardjewelry.com/cart';
  });
});
</script>

 

Link to comment
  • Replies 5
  • Views 1.3k
  • Created
  • Last Reply

There’s an Express Checkout option on all stores and when enabled, it will always send customers to the checkout when they add an item to the cart. You can read more here:

https://support.squarespace.com/hc/en-us/articles/206540817-Express-Checkout
 

If you need something slightly different, it can be achieved with JavaScript. As you’ve noted, the ‘Add to Cart’ button’s label changes to ‘added’ when the action has completed. This means it is a useful event to use to redirect the visitor. There are a number of ways to do this. Originally suggested by @brandon, one method is to use a MutationObserver to check when the class ‘.cart-added‘ is added to ’.sqs-add-to-cart-button’, triggering a redirect to the cart.

 

About me: I've been a SQSP User for 18 yrs. I was invited to join the Circle when it launched in 2016. I have been a Circle Leader since 2017. I don't work for Squarespace. I value honesty, transparency, diversity and good design ♥.
Work: I founded and run SF.DIGITAL, building Squarespace Extensions to supercharge your commerce website. 
Content: Views and opinions are my own. Links in my posts may refer to SF.DIGITAL products or may be affiliate links.
Forum advice is free. You can thank me by clicking one of the feedback emojis below. Coffee is optional.

Link to comment

Alright, here's a repost for those that cannot access that particular post:

Quote

 

The following script, inserted via sitewide footer code injection seems to work for me, in my brief tests:

Option 1: Redirect.


<script>
  // When a product is added to cart, redirect user to /cart page.
  (function() {
    var cartBtns = document.querySelectorAll(".sqs-add-to-cart-button:not(.cart-added)");
    var i;
    for (i = cartBtns.length - 1; i >= 0; i--) {
      new MutationObserver(function(mutations) {
        if (mutations[0].target.classList.contains("cart-added")) {
          window.location.href = "/cart";
        }
      }).observe(cartBtns[i], {
        attributes: true,
        attributeFilter: ["class"]
      });
    }
  })();
</script>

 

Option 2: Prompt.


<script>
  // When a product is added to cart, add a prompt below the button with links to other actions.
  (function() {
    var cartBtns = document.querySelectorAll(".sqs-add-to-cart-button:not(.cart-added)");
    var i;
    for (i = cartBtns.length - 1; i >= 0; i--) {
      new MutationObserver(function(mutations) {
        if (mutations[0].target.classList.contains("cart-added")) {
          var pd = document.getElementById("productDetails");
          var prompt;
          if (pd) {
            prompt = document.createElement("div");
            prompt.innerHTML = "<p>Item added to cart. <a href='/cart'><b>Go to Cart</b></a> or <a href='/order/cmor-map-pak'><b>Add Display Device</b></a>.</p>";
            pd.appendChild(prompt);
          };
          this.disconnect();
        }
      }).observe(cartBtns[i], {
        attributes: true,
        attributeFilter: ["class"]
      });
    }
  })();
</script>

There may be some testing and tweaks necessary for various scenarios, but it’s a good start.

(Side note: This is one of those instances where I have the feeling there are a number of different ways to go about this, elements/data you could watch-/listen-for, etc. This is just one method that focuses on the user interaction with the add-to-cart button.)

Let us know how it goes.

-Brandon

 

 

If a response helped you out, send a 'Like' 👍 (bottom-right) and/or 'Upvote' vote.jpg.c260784ece77aec852b0e3049c77a607.jpg (top-left)

Link to comment

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.