I’m building a theme with Slate and I have been researching how to prevent the default function of going to the cart page after you click add to cart on a product page.
All the answers I have gotten thus far have lead to dead ends. I also tried to load Cart.js onto the theme and it didn’t let me because there’s some liquid code mixed in with the initialize script.
Really looking for help to prevent a theme built with Slate from automatically going to the cart page once you click add to cart. Thanks!
2
Answers
Found a solution using Ajaxify cart (https://help.shopify.com/themes/customization/products/add-to-cart/stay-on-product-page-when-items-added-to-cart)
To get this to work with Slate, you need to follow the instructions and make a few changes once they are all followed. Here's what I did.
I had to take all the jQuery in the script tags and place it in a new file under scripts > vendor > vendor.js
Next I made sure that this file was being called to the main vendor.js file using this code in scripts > vendor.js
Last thing I had to do was edit out the liquid markup that was in the ajaxify-cart.js file. Since it is a .js file the liquid markup was making it malfunction. Here's the lines where I replaced liquid markup:
It's a bit hacky but so far it is working with my slate theme.
I'm open to suggestions for improvement. Thanks.
The redirect is probably based on a form submission, so you just need to use jQuery’s preventDefault method when the form is submitted.