After checking out successfully, I go back to Shopify store to proceed new purchase. However, the cart is not cleared.
Hey @aimeehoang
Try below code in your theme’s theme.liquid or thank-you.liquid template, ensuring it runs only after checkout.
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
// Check if returning from checkout
if (params.has('checkout') && params.get('checkout') === 'success') {
fetch('/cart/clear.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
if (response.ok) {
console.log('Cart cleared successfully.');
} else {
console.error('Failed to clear the cart.');
}
}).catch(error => console.error('Error clearing the cart:', error));
}
});