486 votes
0 answers
Shopify, Liquid, Alpine JS, Ajax can't redirect to cart after firing addToCart
Solution:
The way I solved this was to use the Location API (https://developer.mozilla.org/en-US/docs/Web/API/Location) add location.href="/cart" in the success promise of the Ajax call.
fetch(window.Shopify.routes.root + 'cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
})
.then(response => {
console.log(response)
return response.json();
})
.then((data) => {
location.href = '/cart';
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
Undefined asked
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
465 api - Is there a way to create a Shopify storefront app that only affects one page of the store?
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.