Skip to content

Basket Promotions API

Feature name: basket

Api name: basketPromotionsApi


API used to perform actions related to promotions in a basket like managing coupon codes or retrieving data related to promotions.

classDiagram
direction LR
    class select["select$"]
    class selectDiscountsSum["selectDiscountsSum$"]
    class selectHasPromotionCode["selectHasPromotionCode$"]
    class selectPromotionCode["selectPromotionCode$"]

    BasketPromotionsAPI --> add
    BasketPromotionsAPI --> remove
    BasketPromotionsAPI --> get
    BasketPromotionsAPI --> select
    BasketPromotionsAPI --> getDiscountsSum
    BasketPromotionsAPI --> selectDiscountsSum
    BasketPromotionsAPI --> getHasPromotionCode
    BasketPromotionsAPI --> selectHasPromotionCode
    BasketPromotionsAPI --> getPromotionCode
    BasketPromotionsAPI --> selectPromotionCode

    class BasketPromotionsAPI {
        add(coupon: string) Promise~boolean~
        remove() Promise~void~
        get() Discount[]
        select$() Observable~Discount[]~
        getDiscountsSum() FullPrice
        selectDiscountsSum$() Observable~FullPrice~
        getHasPromotionCode() boolean
        selectHasPromotionCode$() Observable~boolean~
        getPromotionCode() PromotionCode | null
        selectPromotionCode$() Observable~PromotionCode | null~
    }

    link add "../methods/add/"
    link remove "../methods/remove/"
    link get "../methods/get/"
    link select "../methods/select/"
    link getDiscountsSum "../methods/get-discounts-sum/"
    link selectDiscountsSum "../methods/select-discounts-sum/"
    link getHasPromotionCode "../methods/get-has-promotion-code/"
    link selectHasPromotionCode "../methods/select-has-promotion-code/"
    link getPromotionCode "../methods/get-promotion-code/"
    link selectPromotionCode "../methods/select-promotion-code/"

Get API

To get the Basket Promotions API use its name basketPromotionsApi with the getApi method.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPromotionsApi = await getApi('basketPromotionsApi');
    });
});

This API is not automatically initialized. Unless you initialize it by hand or use a proper event like in the example above, you won't be able to fetch the API. Read more in the Retrieving basket APIs section

Methods

  • add - add a coupon code to the basket
  • remove - remove a coupon code from the basket
  • get - retrieve a list of active discounts in the basket
  • select$ - select a list of active discounts in the basket
  • getDiscountsSum - retrieve a sum of all discounts in the basket
  • selectDiscountsSum$ - select a sum of all discounts in the basket
  • getHasPromotionCode - retrieve the information about whether a promotion code is present in the basket or not
  • selectHasPromotionCode$ - select the information about whether a promotion code is present in the basket or not
  • getPromotionCode - retrieve the active promotion code in the basket
  • selectPromotionCode$ - select the active promotion code in the basket

Event Bus events

This API listens to the following events with the Event Bus:

Example

In this example we make a basketPromotionsApi call to add a coupon code to the basket.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPromotionsApi = await getApi('basketPromotionsApi');

        basketPromotionsApi.add('examplecoupon');
    });
});

Example

In this example we make a basketPromotionsApi call to retrieve a list of active discounts in the basket.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPromotionsApi = await getApi('basketPromotionsApi');

        const discountsList = basketPromotionsApi.get();
    });
});

Example

In this example we make a basketPromotionsApi call to select a sum of all discounts in the basket.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPromotionsApi = await getApi('basketPromotionsApi');

        const discountsSum$ = basketPromotionsApi.selectDiscountsSum$();
    });
});

Example

In this example we make a basketPromotionsApi call to select the active promotion code in the basket.

useStorefront(async ({ eventBus, getApi }) => {
    eventBus.on('basket.initialized', async () => {
        const basketPromotionsApi = await getApi('basketPromotionsApi');

        const promotionCode$ = basketPromotionsApi.selectPromotionCode$();
    });
});

Models reference

APIs reference