basket.itemAddedToBasket¶
basket.itemAddedToBasket
is an event on the Event Bus that occurs whenever an item is added to the basket.
Event body¶
The basket.itemAddedToBasket
body has a TItemAddedToBasketEvent type which represents the item that has been added to the basket. It is optional so it might not exist.
Example¶
In this example we listen to basket.itemAddedToBasket
event and perform an action whenever it's emitted.
useStorefront(async (storefront) => {
storefront.eventBus.on('basket.itemAddedToBasket', ({ body: itemAddedToBasket }) => {
console.log('An item has been added to basket:', itemAddedToBasket);
});
});
Example¶
In this example we use a Message Storage API to retrieve an array of past basket.itemAddedToBasket
events.
useStorefront((storefront) => {
const messageStorageApi = storefront.getApiSync('messageStorageSystemApi');
const pastItemAddedToBasketEvents = messageStorageApi.getChannelMessages('basket.itemAddedToBasket');
if (pastItemAddedToBasketEvents.length > 0) {
pastItemAddedToBasketEvents.forEach(({ body: eventBody }) => {
console.log('perform action on every past event');
});
}
});