I’m working on a Shopify store and trying to customize the Ajax functionality for adding products to cart. I want the success notification to appear on the cart page after redirecting, but I’m having trouble making it work.
function handleCartAddition(response, status, error) {
$.ajax({
method: 'GET',
url: '/cart.js',
async: false,
cache: false,
dataType: 'json',
success: refreshCartInfo
});
window.location = "/cart";
$('.cart-notification').hide().addClass('success-msg').html('Product added successfully! <a href="/cart">Go to cart</a>').fadeIn(300);
}
The message shows fine on the product page without the redirect line, but when I add window.location = "/cart" the notification doesn’t display on the cart page. I also tried making a custom function but I’m not experienced with Ajax:
function refreshCartInfo(response, status, error) {
$.ajax({
method: 'POST',
url: '/cart',
async: false,
cache: false,
dataType: 'html',
success: function() {
$('.cart-notification').hide().addClass('success-msg').html('Product added successfully!').fadeIn(300);
}
});
}
Can someone help me understand what I’m doing wrong? Any resources or examples would be great.