Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Latest commit

 

History

History
61 lines (34 loc) · 4.66 KB

File metadata and controls

61 lines (34 loc) · 4.66 KB

📣 Announcement: New documentation location

The documentation for WooCommerce Blocks has moved to the WooCommerce monorepo.

Please refer to the documentation in the new location as the files in this repository will no longer be updated and the repository will be archived.


DOM Events

Table of Contents

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 event is the equivalent to the jQuery event added_to_cart triggered by WooCommerce core. It indicates that the process of adding a product to the cart has finished with success.

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.

detail parameters

Parameter Type Default value Description
preserveCartData boolean false Whether Cart data in the store should be preserved. By default, it's false so any wc-blocks_added_to_cart event will invalidate cart data and blocks listening to it will refetch cart data again. However, if the code triggering the event already updates the store (ie: All Products block), it can set preserveCartData: true to avoid the other blocks refetching the data again.

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.


We're hiring! Come work with us!

🐞 Found a mistake, or have a suggestion? Leave feedback about this document here.