JavaScript events

Cart events

Reference for the cart lifecycle CustomEvents Llama Upsells dispatches on document, with exact event names, payloads, and listener examples.

Llama Upsells keeps your theme in sync with cart state by dispatching DOM CustomEvents on document whenever the cart changes. You listen for these events to react to add-to-cart actions, cart updates, discount-code changes, and Llama Cart toggles — without polling the Shopify cart yourself.

All events are dispatched on document, so attach listeners with document.addEventListener(...). Each event name is a fixed string; the tables and examples below use the exact value Llama Upsells emits.

Event payloads ride on the standard event.detail property. Always read data from event.detail, and guard against missing fields — some events fire before the related fetch resolves.

Event reference

ConstantEvent nameFires when
CART_UPDATEDupsell:cart-updatedThe Shopify cart changes (item added, removed, or quantity updated).
STOREFRONT_CART_UPDATEDupsell:storefront-cart-updatedThe Storefront API cart is refetched after a cart or discount change.
SMART_CART_UPDATEDlcu:smart-cart:updatedThe Llama (Smart) Cart state updates.
CART_DISCOUNT_CODES_UPDATEDupsell:cart-discount-codes-updatedApplied discount codes change (a code is applied or removed).
ITEMS_ADDED_TO_CARTlcu:cart:item_addedOne or more line items are added to the cart.
ITEMS_REMOVED_FROM_CARTlcu:cart:item_removedOne or more line items are removed from the cart.
LLAMA_CART_TOGGLElcu:cart:toggleThe Llama Cart is toggled open or closed.

upsell:cart-updated

The primary cart event. Llama Upsells dispatches it whenever it detects the Shopify cart has changed, passing the full updated cart object.

event.detail

FieldTypeDescription
newCartobjectThe updated Shopify cart, in the same shape as /cart.js.
document.addEventListener('upsell:cart-updated', (event) => {
  const { newCart } = event.detail;

  console.log('Item count:', newCart.item_count);
  console.log('Line items:', newCart.items);
});

upsell:cart-updated is the event most integrations need. When it fires, Llama Upsells also kicks off a Storefront API refetch and follows up with upsell:storefront-cart-updated once that resolves.

upsell:storefront-cart-updated

Fires after Llama Upsells refetches the cart through the Shopify Storefront API. This carries the richer Storefront cart representation (including discount data), which the standard /cart.js payload does not provide.

It is dispatched in two situations:

  • After upsell:cart-updated, once the follow-up Storefront fetch resolves (skipped if the cart has no token or no items array).
  • After a discount code is applied or removed, with the cart returned by the discount mutation.

event.detail

FieldTypeDescription
newStorefrontCartobjectThe cart as returned by the Storefront API, including discountCodes and discountAllocations.
document.addEventListener('upsell:storefront-cart-updated', (event) => {
  const { newStorefrontCart } = event.detail;

  console.log('Storefront cart:', newStorefrontCart);
  console.log('Discount codes:', newStorefrontCart.discountCodes);
});

lcu:smart-cart:updated

Fires when the Llama (Smart) Cart's internal state updates. Use it to react to changes in the rendered Llama Cart specifically, rather than the underlying Shopify cart.

document.addEventListener('lcu:smart-cart:updated', (event) => {
  // React to the latest Llama Cart state.
  console.log('Smart cart updated', event.detail);
});

Read any data you need from event.detail, and check that the field you expect is present before using it.

upsell:cart-discount-codes-updated

Fires when the set of applied discount codes changes — for example, after a shopper applies or removes a code in the Llama Cart and Llama Upsells revalidates the cart against your campaigns.

event.detail

FieldTypeDescription
newCartobjectThe current Shopify cart object.
discountCodesstring[]The list of applicable discount codes now on the cart.
callbackfunctionAn internal callback used by Llama Upsells to validate fetched line items against applied promo codes.
document.addEventListener('upsell:cart-discount-codes-updated', (event) => {
  const { newCart, discountCodes } = event.detail;

  console.log('Applied discount codes:', discountCodes);
  console.log('Cart total:', newCart.total_price);
});

The callback on this event is for Llama Upsells' internal discount validation. Read newCart and discountCodes, but do not call or override callback.

lcu:cart:item_added

Fires when one or more line items are added to the cart. Use it to trigger your own analytics, animations, or side effects on add-to-cart.

document.addEventListener('lcu:cart:item_added', (event) => {
  // Items were just added to the cart.
  console.log('Items added', event.detail);
});

lcu:cart:item_removed

Fires when one or more line items are removed from the cart.

document.addEventListener('lcu:cart:item_removed', (event) => {
  // Items were just removed from the cart.
  console.log('Items removed', event.detail);
});

lcu:cart:toggle

Fires when the Llama Cart is toggled — opened or closed. Use it to keep your own UI in sync with the cart drawer's open state.

document.addEventListener('lcu:cart:toggle', (event) => {
  // The Llama Cart was opened or closed.
  console.log('Llama Cart toggled', event.detail);
});

Putting it together

A common pattern is to listen for upsell:cart-updated to react to any cart change, then use upsell:storefront-cart-updated when you specifically need discount information. Remember to remove your listeners when they are no longer needed.

function onCartUpdated(event) {
  const { newCart } = event.detail;
  // Update your UI with the latest cart.
  render(newCart);
}

function onStorefrontCartUpdated(event) {
  const { newStorefrontCart } = event.detail;
  // Update discount-aware UI.
  renderDiscounts(newStorefrontCart.discountCodes);
}

document.addEventListener('upsell:cart-updated', onCartUpdated);
document.addEventListener('upsell:storefront-cart-updated', onStorefrontCartUpdated);

// Later, when tearing down:
document.removeEventListener('upsell:cart-updated', onCartUpdated);
document.removeEventListener('upsell:storefront-cart-updated', onStorefrontCartUpdated);

Cart event listeners are safe to attach as soon as your script runs. Llama Upsells dispatches these events on document throughout the page lifecycle, so a listener added early will catch later updates.

On this page