Code and developers

JavaScript events for developers

Shopify standard events and actions in Krown themes, the theme's own cart, variant and filter events, and how to refresh the cart from your own code.

Krown support does not provide help with custom code. This page is for developers who connect an app or their own script to a Krown theme.

Krown themes use Shopify's standard events and actions, the storefront events that analytics, tracking and cart apps listen to. Apps built for them work with a Krown theme without extra code. Each theme also keeps its own events from before, so older scripts keep working. Every theme is written in plain JavaScript, with no jQuery or framework.

Which themes send Shopify standard events?

Every Krown theme, from these versions:

ThemeStandard events since
Seventh3.0.0
Local4.1.2
Combine4.1.0
Borders3.0.0
Split5.1.0
Highlight4.1.0
Kingdom6.2.0

Update your theme to get them: see Update your Shopify theme.

Shopify standard events

The events bubble, so one listener on document receives them all. Shopify documents each event and its data in the storefront events reference.

document.addEventListener('shopify:cart:lines-update', (event) => {
  // a product was added, or a cart line was changed or removed
});

Krown themes send:

  • shopify:page:view: once on every page.
  • shopify:product:view: on the product page and in the Quick view popup.
  • shopify:product:select: when a customer selects another variant.
  • shopify:collection:view: on the collection page.
  • shopify:collection:update: when a customer filters or sorts a collection.
  • shopify:search:update: when a customer filters or sorts search results.
  • shopify:cart:view: on the cart page, and when the cart drawer opens.
  • shopify:cart:lines-update: when a product is added to the cart, or a cart line changes or is removed.
  • shopify:cart:note-update: when the cart note changes.
  • shopify:cart:discount-update: when a discount code is applied or removed in the cart.
  • shopify:cart:error: when Shopify rejects a cart change, for example a quantity higher than the stock.

What to know before you use them

  • Borders does not send view events: no shopify:product:view, shopify:collection:view or shopify:cart:view. Every other event in the list works in Borders.
  • No theme sends shopify:cart:attributes-update. The themes do not change cart attributes from the storefront.
  • Cart events fire after the cart has changed. The event's promise is already resolved and holds no cart ({ cart: null }). To read the new cart, fetch /cart.js in your listener.
  • The library loads at the end of the page. The theme reads Shopify's library from window.Shopify.StandardEvents. Add your listeners early, in a script in the page head or an app embed, so you do not miss shopify:page:view.

Shopify actions: open and update the cart

Apps change the cart through Shopify's storefront actions. Every Krown theme connects them to its own cart:

  • Shopify.actions.openCart() opens the cart drawer. When the theme uses the cart page instead of a drawer, it opens the cart page.
  • Shopify.actions.updateCart(...) changes the cart, then refreshes the cart drawer and the cart count in the header without reloading the page. The standard cart events fire on their own: do not dispatch them again.
  • Shopify.actions.getCart() returns the cart. It is Shopify's own action; themes cannot change it.
await Shopify.actions.updateCart({ lines: [{ merchandiseId: 'gid://shopify/ProductVariant/123', quantity: 1 }] });
Shopify.actions.openCart();

The theme's own buttons (add to cart, quantity, remove) do not go through these actions: they call Shopify's Ajax cart API and send the standard events themselves.

Refresh the cart after an Ajax change

When your code changes the cart through Shopify's Ajax API instead of Shopify.actions.updateCart, refresh the cart drawer and the cart count:

window.refreshCart(true);  // refresh and open the cart drawer
window.refreshCart(false); // refresh without opening it
  • Highlight and Kingdom: window.refreshCart() takes no argument and always opens the cart drawer. In Highlight it needs Cart type set to Drawer.
  • Seventh: KROWN.functions.updateCart(openDrawer) does the same as window.refreshCart(openDrawer). KROWN.functions.openCartDrawer() and KROWN.functions.closeCartDrawer() open and close the drawer.

Krown theme events

Each theme still fires its own events from before the standard events. They are CustomEvents dispatched on document with bubbles: true, and their data is in event.detail.

document.addEventListener('krown:variant:change', (event) => {
  const { variant, productForm } = event.detail;
});

krown:variant:change fires when a customer selects a variant in any variant picker of the theme, for example on the product page or in the Quick view popup.

  • variant: the selected variant object.
  • productForm: the product form element.

krown:cart:add fires after a product form adds a variant to the cart.

  • productForm: the form element.
  • variantId: the ID of the variant that was added.

krown:cart:update fires when a customer changes the quantity of a cart item, or removes it.

  • itemKey: the line item key.
  • itemQty: the new quantity.
  • itemVariantId: the variant ID.
  • itemProductId: the product ID.

krown:cart:error fires when Shopify rejects a cart quantity change.

  • error: the error text returned by Shopify.

krown:main-collection-grid:filtered fires after a customer filters or sorts the collection grid. The theme replaces the grid without reloading the page, so code that runs on product cards must run again after this event.

  • gridElement: the product grid element.

Seventh only

krown:cart-sidebar:show and krown:cart-sidebar:hide fire when the cart drawer opens and closes.

  • sidebarElement: the cart drawer element.

krown:product-modal:init fires once per product, the first time it loads in the Quick view popup.

  • productId: the product ID.
  • variant: the selected variant object.

krown:product-modal:show fires each time the Quick view popup opens.

  • modalElement: the Quick view popup element.

Element events

The oldest events still fire on the theme's custom elements, in every theme: cart-updated on cart-form (an item is removed or its quantity changes), add-to-cart on product-form, and VARIANT_CHANGE on product-variants (read the selected variant from its currentVariant property). Use the krown: events or the standard events in new code.

On this page