Listeners

Commerce broadcasts events when changes occur to the internal state.

To listen for these events you must first ensure that Commerce is ready.

Once you have determined that Commerce is ready, you can then listen for other events using subscribe or access the methods.

commerce.ready

If your own code is loading before Commerce, you can wait until Commerce is ready by listening for the commerce.ready event.

This event will be broadcast only once as a standard event to the document.

To listen for commerce.ready:

document.addEventListener( 'commerce.ready', function () {
  // Commerce is now available
} )

If your own code is loading after Commerce, you can check for the existence of window.Commerce.

subscribe

Once Commerce is ready (see: commerce.ready) you can use subscribe to listen to the events published by Commerce.

Commerce.subscribe( 'event.type', function () {
  // The event has occured here
} )

unsubscribe

Once you have subscribed to an event, you can unsubscribe in several ways.

To unsubscribe from a single specific listener, pass the id received when initially subscribing:

let id = Commerce.subscribe( 'event.type', function () {
  // The event has occured here
} )
Commerce.unsubscribe( id )

To unsubscribe from all listeners of an event, pass only the event type:

Commerce.unsubscribe( 'event.type' )