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~void~
        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 getApiSync method.

useStorefront(async (storefront) => {
    const basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');
});

This API is not automatically initialized. Unless you initialize it by hand, you won't be able to fetch the API. To do it you can use the registerDynamic method from the Feature System API. Here is an example on how to do it:

useStorefront(async (storefront) => {
    const featureSystemApi = this.getApiSync('FeatureSystemApi');
    await featureSystemApi.registerDynamic('basket');

    const basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');
});

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 (storefront) => {
    let basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');

    if (!basketPromotionsApi) {
        const featureSystemApi = this.getApiSync('FeatureSystemApi');
        await featureSystemApi.registerDynamic('basket');

        basketPromotionsApi = storefront.getApiSync('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 (storefront) => {
    let basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');

    if (!basketPromotionsApi) {
        const featureSystemApi = this.getApiSync('FeatureSystemApi');
        await featureSystemApi.registerDynamic('basket');

        basketPromotionsApi = storefront.getApiSync('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 (storefront) => {
    let basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');

    if (!basketPromotionsApi) {
        const featureSystemApi = this.getApiSync('FeatureSystemApi');
        await featureSystemApi.registerDynamic('basket');

        basketPromotionsApi = storefront.getApiSync('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 (storefront) => {
    let basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');

    if (!basketPromotionsApi) {
        const featureSystemApi = this.getApiSync('FeatureSystemApi');
        await featureSystemApi.registerDynamic('basket');

        basketPromotionsApi = storefront.getApiSync('basketPromotionsApi');
    }

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

Models reference

APIs reference