Skip to main content
All CollectionsTroubleshooting
Cart Icon Quantity/Counter Not Updating
Cart Icon Quantity/Counter Not Updating

How to fix the cart icon showing the wrong number of items after adding or removing items.

Updated over 5 months ago

Problem

UpCart automatically updates the quantity in the cart icon for most themes. However, themes can vary widely and UpCart is not able to automatically update the quantity for all themes. If the quantity shown in UpCart is different than the quantity shown in your theme icon, then continue reading for a solution.

In the following example image, the cart icon shows that the customer has 1 item in their cart, but UpCart shows that there are actually 2 items in their cart. This most commonly happens after the customer has added or removed an item from their cart. This problem often disappears after refreshing the page.

A store with the cart icon showing a different quantity than UpCart.

Solution

There are multiple levels of solutions depending on the complexity of your cart icon. Each level builds on the previous level, so it is recommended to watch Level 1 first and move to the next level only if needed.

Level 1

Example Code for Level 1

<script>
window.upcartOnCartLoaded = (cart) => {
const countEl = document.querySelector('.cart-count-bubble > span');
countEl.innerText = cart.item_count;
}
</script>

Note that we do not need the window.upcartOnCartUpdated function because window.upcartOnCartLoaded is called on every update to the cart. This was a mistake in the video.

Level 2

Example Code for Level 2

<script>
;(() => {
function updateCartCount(cart) {
const iconWrapperEl = document.querySelector('#cart-icon-bubble-custom');
if (cart.item_count === 0) {
iconWrapperEl.innerHTML = `<span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart-empty" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M15.75 11.8h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33L28.4 11.8zm0 1h-2.22l-.71 10.67a4 4 0 0 0 3.99 4.27h7.38a4 4 0 0 0 4-4.27l-.72-10.67h-2.22v.63a4.75 4.75 0 1 1-9.5 0zm8.5 0h-7.5v.63a3.75 3.75 0 1 0 7.5 0z"></path></svg></span><span class="visually-hidden">Cart</span>`;
} else {
iconWrapperEl.innerHTML = `<span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M20.5 6.5a4.75 4.75 0 0 0-4.75 4.75v.56h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33l-.77-11.6h-3.16v-.57A4.75 4.75 0 0 0 20.5 6.5m3.75 5.31v-.56a3.75 3.75 0 1 0-7.5 0v.56zm-7.5 1h7.5v.56a3.75 3.75 0 1 1-7.5 0zm-1 0v.56a4.75 4.75 0 1 0 9.5 0v-.56h2.22l.71 10.67a4 4 0 0 1-3.99 4.27h-7.38a4 4 0 0 1-4-4.27l.72-10.67z"></path></svg></span><span class="visually-hidden">Cart</span><div class="cart-count-bubble"><span aria-hidden="true">${cart.item_count}</span><span class="visually-hidden">${cart.item_count} item</span></div>`;
}
}

window.upcartOnCartLoaded = (cart) => {
updateCartCount(cart);
}
})();
</script>

Note that we do not need the window.upcartOnCartUpdated function because window.upcartOnCartLoaded is called on every update to the cart. This was a mistake in the video.

Level 3

Example Code for Level 3

<script>
;(() => {
function updateCartCount(cart) {
// Desktop
const desktopIconWrapperEl = document.querySelector('#cart-icon-bubble-custom-desktop');
if (cart.item_count === 0) {
desktopIconWrapperEl.innerHTML = `<span>Cart</span><span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart-empty" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M15.75 11.8h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33L28.4 11.8zm0 1h-2.22l-.71 10.67a4 4 0 0 0 3.99 4.27h7.38a4 4 0 0 0 4-4.27l-.72-10.67h-2.22v.63a4.75 4.75 0 1 1-9.5 0zm8.5 0h-7.5v.63a3.75 3.75 0 1 0 7.5 0z"></path></svg></span>`;
} else {
desktopIconWrapperEl.innerHTML = `<span>Cart (${cart.item_count})</span><span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M20.5 6.5a4.75 4.75 0 0 0-4.75 4.75v.56h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33l-.77-11.6h-3.16v-.57A4.75 4.75 0 0 0 20.5 6.5m3.75 5.31v-.56a3.75 3.75 0 1 0-7.5 0v.56zm-7.5 1h7.5v.56a3.75 3.75 0 1 1-7.5 0zm-1 0v.56a4.75 4.75 0 1 0 9.5 0v-.56h2.22l.71 10.67a4 4 0 0 1-3.99 4.27h-7.38a4 4 0 0 1-4-4.27l.72-10.67z"></path></svg></span>`;
}

// Mobile
const mobileIconWrapperEl = document.querySelector('#cart-icon-bubble-custom-mobile');
if (cart.item_count === 0) {
mobileIconWrapperEl.innerHTML = `<span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart-empty" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M15.75 11.8h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33L28.4 11.8zm0 1h-2.22l-.71 10.67a4 4 0 0 0 3.99 4.27h7.38a4 4 0 0 0 4-4.27l-.72-10.67h-2.22v.63a4.75 4.75 0 1 1-9.5 0zm8.5 0h-7.5v.63a3.75 3.75 0 1 0 7.5 0z"></path></svg></span><span class="visually-hidden">Cart</span>`;
} else {
mobileIconWrapperEl.innerHTML = `<span class="svg-wrapper"><svg xmlns="http://www.w3.org/2000/svg" fill="none" class="icon icon-cart" viewBox="0 0 40 40"><path fill="currentColor" fill-rule="evenodd" d="M20.5 6.5a4.75 4.75 0 0 0-4.75 4.75v.56h-3.16l-.77 11.6a5 5 0 0 0 4.99 5.34h7.38a5 5 0 0 0 4.99-5.33l-.77-11.6h-3.16v-.57A4.75 4.75 0 0 0 20.5 6.5m3.75 5.31v-.56a3.75 3.75 0 1 0-7.5 0v.56zm-7.5 1h7.5v.56a3.75 3.75 0 1 1-7.5 0zm-1 0v.56a4.75 4.75 0 1 0 9.5 0v-.56h2.22l.71 10.67a4 4 0 0 1-3.99 4.27h-7.38a4 4 0 0 1-4-4.27l.72-10.67z"></path></svg></span><span class="visually-hidden">Cart</span><div class="cart-count-bubble"><span aria-hidden="true">${cart.item_count}</span><span class="visually-hidden">${cart.item_count} item</span></div>`;
}
}

window.upcartOnCartLoaded = (cart) => {
updateCartCount(cart);
}
})();
</script>

Note that we do not need the window.upcartOnCartUpdated function because window.upcartOnCartLoaded is called on every update to the cart. This was a mistake in the video.

Did this answer your question?