Update a server-side render Block when woocommerce cart block changed

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.

techhipbettruvabetnorabahisbahis forumutaraftarium24eduedusedusedueduseduedueduseduedus