In Shopify, how do I refresh the cart after applying discount code?

I could use some help. We're using Dawn theme. My goal is to auto-apply a discount code to cart to cart for only certain customers which I will eventually control with a tag or metafield. I can successfully auto-apply the discount code, but I can't figure out how to refresh the cart contents showing the product discount without reloading the whole page. Here's the code: const discountCode = 'PSR'; fetch(`/discount/${discountCode}`) .then(() => { // After the discount code has been applied, force the cart to update // so the discount is visible return fetch('/cart/update.js', { method: 'POST', body: JSON.stringify({ updates: { [discountCode]: 1 } }), headers: { 'Content-Type': 'application/json', }, }); }) .then(() => { // Refresh cart contents to show discount document.dispatchEvent(new CustomEvent('cart:build', { bubbles: true })); document.dispatchEvent(new CustomEvent('cart:refresh', { bubbles: true, detail: cartData })); }); What am I doing wrong here? Any help is appreciated!

Comment (0)

You’ll be in good company