LogoLogo
HomeTheme DocsSupportTry Primavera
  • Primavera Theme Docs
  • Guides
    • Quick start
    • Accessibility
    • SEO
    • How to set up the same homepage sections as the demo stores
    • How to add a mega menu
    • How to add a sidebar menu promotion
    • How to add product swatches
    • How to add a product subtitle/cutline
    • How to add product badges
    • How to add product recommendations
    • How to add an external video
    • How do I enable right-to-left support
  • Theme settings
    • Spacing
    • Appearance
    • Logo
    • Colors
    • Typography
    • Product card
    • Mini product
    • Collection card
    • Blog post card
    • Search behavaior
    • Social media
    • Currency format
    • Cart
    • Optimizations
    • Custom CSS
  • Sections
    • Announcement bar
    • Back to top
    • Banner grid
    • Before/after image
    • Bottom bar
    • Blog posts
    • Cart drawer
    • Collapsible content
    • Collection list
    • Contact form
    • Countdown timer
    • Custom liquid
    • External video
    • Featured collection
    • Featured post
    • Featured product
    • Footer
    • Header
    • Image banner
    • Image hotspot
    • Image with text
    • Logo list
    • Media grid
    • Multicolumn
    • Newsletter
    • Newsletter popup
    • Rich text
    • Rich text columns
    • Scrolling content
    • Search modal
    • Section header
    • Shoppable banner
    • Sidebar menu
    • Slideshow
    • Split banner
    • Split card
    • Stacked images with text
    • Testimonials
    • Video banner
    • Video with text
  • Blocks
    • Badges
    • Buttons
    • Card
    • Collapsible
    • Heading
    • Image
    • Liquid
    • Mini product
    • Page
    • Product
    • Text
    • Video
  • Templates
    • 404
    • Account
    • Blog
    • Blog post
    • Cart page
    • Collection
    • Collection list
    • Gift card
    • Home page
    • Product
    • Password
    • Search
  • FAQs
    • Why isn't my video autoplaying?
    • How do I edit the default theme content?
    • Where do newsletter signups go?
    • Why are my images cropped?
    • How can I change the meta color?
  • Advanced
    • Customizing code
    • JavaScript events
    • How to refresh the cart using JavaScript
    • Use a custom font
  • Contact support
Powered by GitBook
On this page
  • Dialog events
  • Quick add
  • Products
  • Cart
  1. Advanced

JavaScript events

This is an advanced customization. We recommend enlisting the help of a developer if you need assistance in implementing this functionality.

Custom JavaScript can be used to hook into existing theme functionality without touching theme code. This page serves as a reference for all the custom events Primavera exposes.

A custom event can be fired by instantiating an instance of the custom event, and then dispatching it.

const event = new CustomEvent('dialog:trigger:cart-drawer');
document.dispatchEvent(event);

You can listen to an event listener by listening to it on the document element, just like any other event.

document.addEventListener('dialog:open:cart-drawer', (event) => {});

Dialog events

All the dialogs in the theme are triggered by the same set of core events.

You can open or close a dialog manually, or listen for existing open close events by replacing the [name] text with the name of the dialog. The name of the dialog can be found in the data-name attribute. So for example, in the following snippet, the name is 'cart-drawer'.

 <custom-dialog 
    data-modal="true" 
    data-overlay="true"
    data-name="cart-drawer" 
    data-section="{{ section.id }}"
    class="cart cart-drawer cart-drawer--{{ section.settings.layout_desktop }}">

This will open a modal. This can be useful if you need to manually open the cart drawer from another app or integration.

new CustomEvent('dialog:trigger:[name]')

This listens for the open event of a modal. You might need to do this if you need to append something to a modals contents once it's open.

document.addEventListener('dialog:open:[name]', (event) => {
    // The target element is the DOM node of the modal
    console.log(event.detail.target)
});

This will force a modal to close.

new CustomEvent(`dialog:force-close:[name]`)

This listens for the close event. You might need to listen for this to do things like reset an animation.

document.addEventListener('dialog:close:[name]', (event) => {
    // The target element is the DOM node of the modal
    console.log(event.detail.target)
});

Quick add

This fires when the quick add is loaded. Maybe useful for appending or removing things to the quick add form dynamically.

document.addEventListener(`dialog:quickadd-loaded`, this.onQuickAddLoaded.bind(this));

Products

This event fires when a variant is changed.

document.addEventListener('variant:change', (event) => {
  // The event detail includes the section id,
  // the full rendered HTML from the section rendering API, and the variant object
  const { sectionId, html, variant } = event.detail;
});

Cart

This event fires when the shopping cart is updated.

document.addEventListener('cart:change', (event) => {
  // The event detail includes the source of the change ('product-form' by default), 
  // the variant id, and the cart response as JSON. 
  const { source, productVariantId, cartData } = event.detail;
});

This event fires when there is a shopping cart error after an fetch event.

document.addEventListener('cart:error', (event) => {
  // The event detail includes the source of the change ('product-form' by default), 
  // the variant id, the response.errors object and an optional message.
  const { source, productVariantId, errors, message } = event.detail;
});

PreviousCustomizing codeNextHow to refresh the cart using JavaScript

Last updated 24 days ago

Need a custom event for your storefront that's not listed here? Just contact and let us know!

our support