Skip to main content

DOM events

Some blocks need to react to certain events in order to display the most up to date data or behave in a certain way. That's the case of the Cart block, for example, that must listen to 'add to cart' events in order to update the cart contents; or the Mini-Cart block, that gets opened every time a product is added to the cart.

WooCommerce core events in WooCommerce Blocks​

WooCommerce core uses jQuery events to trigger and listen to certain events, like when a product is added or removed from the cart. In WooCommerce Blocks, we moved away from using jQuery, but we still need to listen to those events. To achieve that, we have a utility named translatejQueryEventToNative() that listens to those jQuery events, and every time one is triggered, it triggers an associated DOM native event (with the wc-blocks_ prefix).

WooCommerce Blocks events​

wc-blocks_adding_to_cart​

This event is the equivalent to the jQuery event adding_to_cart triggered by WooCommerce core. It indicates that the process of adding a product to the cart was sent to the server, but there is still no indication on whether the product was successfully added or not.

Example usage in WC Blocks: Mini-Cart block listens to this event to append its dependencies.

wc-blocks_added_to_cart​

This native event has several producers. Its cadence and meaning depend on the path that dispatched it; it does not always confirm that a particular add succeeded and is not guaranteed to fire only once for an underlying action.

Example usage in WC Blocks: Cart block (via the useStoreCart() hook) listens to this event to know if it needs to update its contents.

Added-event paths​

PathTrigger and cadenceTarget and optionsdetail and meaning
Interactivity API (iAPI) cart cycleOnce after a settled mutation cycle that contains at least one successful add-origin mutation. It does not fire after an all-failed cycle or a successful remove-only cycle.document.body; bubbles; cancelable{ preserveCartData: true }. This is an aggregate cycle signal, not an acknowledgement for a particular caller.
Classic @wordpress/data addItemToCart thunkAfter each successful call. A caught API failure does not dispatch it.document.body; bubbles; cancelable{ preserveCartData: true }.
jQuery added_to_cart bridgesEach active bridge registration translates every underlying jQuery event. Up to three registrations can be active at once: the Cart block's, the shared useStoreCart() one (a single registration while Cart, Checkout, or any other block using the hook is mounted), and the Mini-Cart's. There is no global exactly-once guarantee.document.body; the Cart and useStoreCart() registrations do not bubble, the Mini-Cart registration bubbles, and none is cancelable{}. This path reports translation of the jQuery event rather than sharing the cadence or payload of the other paths.

The direct iAPI and classic @wordpress/data paths have this envelope:

new CustomEvent( 'wc-blocks_added_to_cart', {
bubbles: true,
cancelable: true,
detail: { preserveCartData: true },
} );

The jQuery bridges use the same event name and target with detail: {} and cancelable: false; only the Mini-Cart registration sets bubbles: true.

For the iAPI path, a mutation cycle can contain work from unrelated callers. One successful add-origin mutation causes the single cycle event even when a sibling mutation from another caller fails. The failed caller still receives a failed addCartItem outcome. A successful keyed quantity update made through addCartItem also has add origin, so this event does not prove that a new cart line was created.

preserveCartData tells listeners whether to retain current cart data. When it is false, listeners can invalidate and refetch cart data. Producers that have already updated the store set it to true to avoid an unnecessary refetch.

wc-blocks_removed_from_cart​

This event is the equivalent to the jQuery event removed_from_cart triggered by WooCommerce core. It indicates that a product has been removed from the cart.

Example usage in WC Blocks: Cart and Mini-Cart blocks (via the useStoreCart() hook) listen to this event to know if they need to update their contents.