Basket¶
To make working with the shopping basket easier, we have split its functions into several smaller APIs. Each API is built to handle one specific part of the process, from adding an item to the cart to completing a purchase.
This structure makes it simpler for you to connect your code. Instead of learning one large, complex API, you can use a specific API for each task. For example, you'll use one API to manage cart items, another to apply discount codes, and a different one for shipping details.
This setup helps you build new features and fix problems more quickly because everything is easier to find. In this section, we will explain each Basket API so you can easily add the functions your store needs.
Basket APIs Explained¶
Here is a breakdown of each specific API and what it helps you do.
Basket Products API¶
Api name: basketProductsApi
This is the main API for managing the items in the cart. You can use it to get a list of all products currently in the basket, see the total number of items, and find out their combined price.
Basket Addresses API¶
Api name: basketAddressesApi
This API lets you manage all customer address information. You can use it to get the current shipping or billing address, update its details, or check that the information is valid.
Basket Shippings API¶
Api name: basketShippingsApi
This API is for managing delivery options. You can get a list of available shipping methods, set the one the customer chooses, or add maps for in-store pickup locations.
Basket Payments API¶
Api name: basketPaymentsApi
This API handles everything related to payments. It lets you get a list of available payment options, save the customer's chosen method, and confirm that the payment section is filled out correctly.
Basket Promotions API¶
Api name: basketPromotionsApi
Use this API to manage discounts and special offers. It lets you add or remove coupon codes from the basket and get information about any promotions that are currently active.
Basket Taxes API¶
Api name: basketTaxesApi
This API is used for all tax-related information. You can get a list of taxes that apply to the items in the basket and see their total calculated amount.
Basket Additional Fields API¶
Api name: basketAdditionalFieldsApi
If your basket uses custom fields (like for gift messages or special requests), this API lets you manage them. You can get a list of the available fields, update their values, and validate the information inside them.
Basket Overall API¶
Api name: basketOverallApi
This API manages the final summary and overall actions for the basket. Use it for things like adding a comment to the order, clearing the entire cart, or getting a final summary of costs and delivery details before checkout.
Retrieving Basket APIs¶
Basket APIs are not automatically initialized hence they need to be manually registered in order to retrieve them. There are two methods of doing so:
First one (recommended) is using the basket.initialized event from Event Bus to ensure the basket feature has been registered.
useStorefront(async ({ eventBus, getApi }) => {
// listen to basket.initialized event
eventBus.on('basket.initialized', async () => {
// retrieve the api of choice
const basketExampleApi = await getApi('basketApiName');
});
});
The second method is manually checking if the API has been retrieved. If it didn't, we manually register the basket feature using the registerDynamic method from Feature System API.
useStorefront(async (storefront) => {
// trying to retrieve the api of choice
let basketExampleApi = await getApi('basketApiName');
if (!basketExampleApi) {
// if it didn't retrieve, manually registering the basket feature
const featureSystemApi = storefront.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('basket');
// then retrieving the api again
basketExampleApi = await getApi('basketApiName');
}
});
NOTE: It's better to use the first method whenever you can as it is far simpler.
APIs reference¶
Below is a complete reference of all Basket APIs and their methods. Click on each API to expand and see its available methods.
Basket Products API
- get - Retrieves a list of items inside the basket
- select$ - Selects a list of items inside the basket
- remove - Removes an item from the basket
- update - Updates the item quantity in basket
- getBasketCount - Retrieves a number of items in the basket
- selectBasketCount$ - Selects a number of items in the basket
- getBasketPositions - Retrieves a number of positions in the basket
- selectBasketPositions$ - Selects a number of positions in the basket
- getSum - Retrieves a sum of all products before discounts and promotions
- selectSum$ - Selects a sum of all products before discounts and promotions
- getDiscountedSum - Retrieves a sum of all products after discounts and promotions
- selectDiscountedSum$ - Selects a sum of all products after discounts and promotions
- getSectionValidation - Validates the products section
- selectSectionValidation$ - Selects validation of the products section
Basket Addresses API
- getShippingAddress - Gets a shipping address in a basket
- selectShippingAddress$ - Selects current shipping address in a basket
- setShippingAddress - Changes a current value of a shipping address
- getBillingAddress - Gets a billing address in a basket
- selectBillingAddress$ - Selects current billing address in a basket
- setBillingAddress - Changes a current value of a billing address
- isBillingAddressEqualShippingAddress - Checks if billing address equals shipping address
- selectIsBillingAddressEqualShippingAddress$ - Selects whether billing address equals shipping address
- isDifferentShippingAddressRequired - Checks if different shipping address is required
- selectIsDifferentShippingAddressRequired$ - Selects whether different shipping address is required
- setBillingAddressAsShippingAddress - Sets billing address as shipping address
- getSectionValidation - Validates the addresses section
- selectSectionValidation$ - Selects validation of the addresses section
Basket Shippings API
- get - Retrieves all basket shipping options
- select$ - Selects basket shipping options
- setShipping - Sets the shipping option for the basket
- setPickupPoint - Sets the pickup point for basket shipping
- getSelectedShipping - Retrieves the currently selected shipping option
- selectSelectedShipping$ - Observes changes to selected shipping option
- getAvailableShippingCountries - Retrieves available shipping countries
- selectAvailableShippingCountries$ - Observes available shipping countries
- getSelectedShippingCountry - Retrieves currently selected shipping country
- selectSelectedShippingCountry$ - Observes changes to selected shipping country
- setCountry - Sets the shipping country by its ID
- getPaymentCostForActiveShippingByPaymentId - Gets payment cost for active shipping by payment ID
- selectPaymentCostForActiveShippingByPaymentId$ - Observes payment cost for active shipping by payment ID
- isEveryAvailablePaymentFree - Checks if every available payment option is free
- selectIsEveryAvailablePaymentFree$ - Observes whether every payment option is free
- getSectionValidation - Validates the shipping section
- selectSectionValidation$ - Observes validation state of shipping section
- addMap - Adds a map for shipping using a specified strategy
- showPickupPointMap - Displays the pickup point map
- shouldDisplayRemainingBasketValueForFreeShippingMessage - Determines if free shipping message should display
- addShippingInfo - Adds shipping information to the shipping option
- getShippingInfo - Retrieves shipping information from the basket
Basket Payments API
- get - Retrieves all basket payment options
- select$ - Selects basket payment options
- getAvailable - Retrieves available payment options
- selectAvailable$ - Observes available payment options
- setPayment - Sets the selected payment option for the basket
- setPaymentData - Sets additional data for a specific payment option
- getSelectedPayment - Retrieves the currently selected payment option
- selectSelectedPayment$ - Observes changes to selected payment option
- getActivePaymentCost - Gets the cost of the active payment option
- selectActivePaymentCost$ - Observes changes to active payment cost
- getPaymentChannelStrategy - Retrieves payment channel strategy for a channel key
- addPaymentChannelStrategy - Adds a new payment channel strategy
- addPaymentInfo - Adds payment information using a template
- getPaymentInfo - Retrieves payment information template
- getSectionValidation - Validates the payment section
- selectSectionValidation$ - Observes validation state of payment section
Basket Promotions API
- add - Adds a coupon code to the basket
- remove - Removes a coupon code from the basket
- get - Retrieves a list of active discounts in the basket
- select$ - Selects a list of active discounts in the basket
- getDiscountsSum - Retrieves a sum of all discounts in the basket
- selectDiscountsSum$ - Selects a sum of all discounts in the basket
- getHasPromotionCode - Retrieves whether a promotion code is present
- selectHasPromotionCode$ - Selects whether a promotion code is present
- getPromotionCode - Retrieves the active promotion code
- selectPromotionCode$ - Selects the active promotion code
Basket Taxes API
- get - Gets a list of taxes associated with current basket
- select$ - Selects a list of taxes associated with current basket
- getSum - Gets a list of tax prices associated with current basket
- selectSum$ - Selects a list of tax prices associated with current basket
Basket Additional Fields API
- get - Gets additional fields in a basket
- select$ - Selects current additional fields in a basket
- change - Changes current value of a given additional field
- upload - Uploads a file into the additional field with file input
- getSectionValidation - Validates the additional fields section
- selectSectionValidation$ - Selects validation of additional fields section
Basket Overall API
- getActions - Retrieves key-value object with various basket actions
- selectActions$ - Selects key-value object with various basket actions
- getId - Retrieves an id of current basket
- selectId$ - Selects an id of current basket
- getComment - Retrieves the text from Comments to order field
- selectComment$ - Selects the text from Comments to order field
- getIsVatEu - Retrieves whether order has EU VAT obligations
- selectIsVatEu$ - Selects whether order has EU VAT obligations
- getDelivery - Retrieves chosen delivery method information
- selectDelivery$ - Selects chosen delivery method information
- getSum - Retrieves total sum of products in basket
- selectSum$ - Selects total sum of products in basket
- getSumWithoutPaymentAndShipping - Retrieves total sum excluding payment and shipping
- selectSumWithoutPaymentAndShipping$ - Selects total sum excluding payment and shipping
- cleanBasket - Clears the products in the current basket
- setComment - Sets a comment to order in the current basket
- getInvalidSections - Retrieves incorrectly filled sections of basket
- selectInvalidSections$ - Selects incorrectly filled sections of basket
- getSectionValidation - Retrieves validation for basket summary section
- selectSectionValidation$ - Selects validation for basket summary section
Basket Updater API
- getBasket - Retrieves the contents of a basket with a given id
- getCurrentBasket - Retrieves the current active basket
- refreshBasket - Refreshes the contents of a basket
- addItem - Adds an item to the basket
- emitUpdateBasketEvent - Emits an update basket event
- uploadVariantFile - Uploads a file for a variant
- placeOrder - Places an order using the basket
- queueOrder - Queues an order for processing
Basket Loyalty API
- selectLoyaltyPointsAwarded$ - Selects the amount of awarded loyalty points
- getLoyaltyPointsAwarded - Retrieves the amount of awarded loyalty points
- selectHasItemsPaidInLoyaltyPoints$ - Selects whether items paid with loyalty points are present
- getHasItemsPaidInLoyaltyPoints - Retrieves whether items paid with loyalty points are present
- selectLoyaltyPointsCost$ - Selects a cost of exchanged items in points
- getLoyaltyPointsCost - Retrieves a cost of exchanged items in points
- selectLoyaltyPointsExchangePrice$ - Selects a price of exchanged items
- getLoyaltyPointsExchangePrice - Retrieves a price of exchanged items