Something like this should work:
const {select, subscribe} = window.wp.data;
const cartStoreKey = window.wc.wcBlocksData.CART_STORE_KEY;
const unsub = subscribe( onCartChange, cartStoreKey );
function onCartChange() {
const cart = select( cartStoreKey ).getCartData();
console.log( '¡ cart change !', cart );
}
The block’s frontend script should have the woocommerce script’s handle as a dependency so the window.wc.wcBlocksData
will be defined by the time the block’s script executes. According to the WC store docs the handle is wc-blocks-data-store
.
In case you haven’t seen it you might want to check out the Interactivity API if the updates you want to make to the markup are non-trivial.
Related Posts:
- How to stop a Gutenberg Block from firing multiple GET Requests?
- Using the WordPress and WooCommerce REST APIs in the same Node app
- List of JS events in the WooCommerce frontend
- WooCommerce: Can’t use wc_get_products for custom REST API endpoints
- Gutenberg custom block plugin with custom image sizes
- Get loading state of wp data selector
- Process checkout using WC REST API
- How to build a plugin that supports authenticated POST requests to the REST API from external servers?
- Get post from custom REST endpoint in Gutenberg
- Extend Woocommerce rest api routes fails
- Hide Add to Cart Button
- Add product attribute to Woocommerce’s blocks in Gutenberg
- WordPress Rest API only returns content when posttype has editor capability
- using woocommerce_template_single_add_to_cart in shop-loop – javascript issues [closed]
- How to filter Post using Meta Data REST API
- Output JSON object with woocommerce products
- Problem with cURL and rest API
- Woo API REST : product variation price is read-only?
- “Cannot read property ‘replace’ of undefined” Javascript error when trying to add attribute terms from add product page [closed]
- Category Tree In WooCommerce Product API
- How to transform a legacy widget into a block
- How to upload Woocommerce product images via API? [closed]
- Javascript / Ajax Error After Migrating To New Domain Name
- How can i stop woocommerce checkout event based on api request result?
- How to handle Woocommerce API credentials
- What will make Woocommerce REST API to issue 401 [closed]
- adding a script with type=”module” to woocommerce admin area
- Woocommerce custom endpoints
- Woocommerce REST API – updating an order changes custom tax calculations
- Force uppercase on some WooCommerce checkout fields input
- Get Block Attributes in JSON REST API
- Woocommerce Rest Api Categories problem [closed]
- Checks when fetching data from multiple REST API endpoints in Gutenberg
- API returns blank response after installing & uninstalling WP-rest-api plugin
- Search for a keyword across post types in a Gutenberg component
- woocommerce axios react
- Using apiFetch for retrieving post data in Gutenberg
- Get specific values in Woocommerce Rest API
- Javascript error when adding an item to the cart [closed]
- wc_get_products() not return the images details
- WooCommerce – Add product with image via REST API
- Tax are not showing on order while placing order with REST API
- JWT on Woocommerce cannot work with “Customer” role user
- Remove WooCommerce block styles
- Get terms of a taxonomy using useSelect
- Woocommerce REST API – add filter to set maximum query per page for product brands [closed]
- WooCommerce – Create multiple product tags via rest api endpoint
- Add to cart – button should be disabled
- Retrieve Data from Custom Endpoint
- Updating failed. The response is not a valid JSON response. Gutenberg + Woocommerce Products
- Woocommerce variable product’s total amount by javascript
- Ajax add to cart not updating Elementor mini cart totals
- Trying to run Javascript on AJAX call
- Enable Gutenberg for Woocommerce
- List of JS events in the WooCommerce frontend
- Woocommerce Checkout Page Total Price Inside Google Pay Script
- Running JavaScript after using WooCommerce Filter Plugin
- Deregister dequeue JS scripts except on 1 product page
- How to solve `Error getting remote image` in woocommerce rest api script?
- why is apiFetch throwing Unhandled Promise Rejection: TypeError: Object is not a function
- woocommerce deposit Form with AJAX and shortcode won’t update but add to previous output
- Getting in an exception for get product detail by id in woocommerce REST api
- How to make wocommerce shop products same heigt
- Impossible to create a new WooCommerce customer using Rest API
- Woocommerce API security concerns
- How can I send information when a order is completed to my own POS?
- Format number to 2 decimal places
- Woocommerce Rest API: woocommerce_product_invalid_image_id
- Update block once an API request returns with a value
- WC Booking query bookings with date ranges
- “error_message”: “Sorry, you cannot list resources.” using WooCommerce API
- currentURL is not defined
- Retrieve Woocommerce Cart Url with javascript/jquery [closed]
- Woocommerce REST API: creating variations
- Woocommerce REST API not considering discounts and coupons
- How to insert link, with lightbox, that opens specific page content only
- Woocommerce custom product with JS script, can’t understand how it works [closed]
- WooCommerce REST API endpoints don’t exist
- How to add my theme’s style to woocommerce blocks?
- Removing ‘woocommerce-no-js’ class from body
- Fetch products with filter by similar categories at same time
- Send data to external websites when there is an update or create a post
- Disable Woocommerce Block Editor
- Auto generating API keys using the Application Authentication Endpoint [closed]
- GET woocommerce order request is not showing meta data [closed]
- How do I fix this “Uncaught ReferenceError: woodmartThemeModule is not defined”?
- Loading specific ajax from a product
- How to add fee_lines using woocommerce rest API v3?
- how to refrence woocommerce blocks in code?
- Embedded data and the _fields parameter in getEntityRecords
- WooCommerce Order Sync via Custom REST API Endpoint Failing
- How to check if is in cart page? [closed]
- Plugin Sidebar is not Saving Meta Attribute to Post/Page after “Update”
- How to build wordpress site which will have location based services & products?
- How to remove product attribute row woocommerce using code
- How to show variations only which are in stock on shop page in woocommerce?
- How to add contact seller button in product page
- Woocommerce Custom Rewrite Rules Array – Subcategories
- Display product variations on same row [closed]
- Remove Ok Button when popup message