I have an issue with my cart drawer that sometimes when I add a product onto my cart in my cart drawer with the add on products, the drawer will automatically close when I click add. I added this javascript down below and I believe it might be the reason I’m having the issue. Is there a way that I can update it where it will still work normally and update my header with the count but not close my drawer randomly?
The issue could also be coming from somewhere else but I’m not sure!
The cart drawer closes because some JS is doing one of these:
Running location.reload()
Redirecting to /cart
Re-rendering the <cart-drawer> element
Using fetch('/cart.js') in a way that forces a hard refresh
The solution is to intercept the Add to Cart event, update the cart asynchronously, and manually update your header count — WITHOUT replacing the drawer DOM.
document.addEventListener(‘click’, async (e) => {
const btn = e.target.closest(‘[data-add-on-button]’);
if (!btn) return; // Only run for your add-on buttons
@userr123 Sometimes the cart drawer closes because the add-to-cart action is refreshing or re-rendering the whole cart instead of just updating the count.
Check your script for anything that forces a reload (like replacing the cart HTML or refreshing the page).
Make sure the code only updates the cart count with AJAX and doesn’t touch the drawer element.
Also look at any recent apps or custom scripts,
some of them override the cart update event and cause the drawer to close randomly.
It sounds like the drawer is closing because your script triggers a full cart update or a form submission that the theme interprets as a page change. Instead, intercept the add-to-cart event with event.preventDefault(), update the cart via AJAX, then update the header count manually. This avoids the drawer closing while keeping functionality intact.