Skip to content

product.stockChanged

product.stockChanged is an event on the Event Bus that occurs whenever the product price changes.

Event body

Body of product.stockChanged event has a ProductStock type which represents the details of a changed product stock. It is optional so it might not exist.

Example

In this example we listen to product.stockChanged event and perform an action whenever it's emitted.

useStorefront(async (storefront) => {
    storefront.eventBus.on('product.stockChanged', ({ body: stock }) => {
        console.log('A stock has changed', stock);
    });
});

Example

In this example we use a Message Storage API to retrieve an array of past product.stockChanged events.

useStorefront((storefront) => {
    const messageStorageApi = storefront.getApiSync('messageStorageSystemApi');

    const pastStockChangedEvents = messageStorageApi.getChannelMessages('product.stockChanged');

    if (pastStockChangedEvents.length > 0) {
        pastStockChangedEvents.forEach(({ body: eventBody }) => {
            console.log('perform action on every past event');
        });
    }
});

JS API reference