addProductsToBasket
Adds new items to a basket.
This method communicates with the addProductsToBasket endpoint of the Vue Storefront API Middleware. In turn, it receives data from the addItemToBasket or the Add items to a basket endpoints exposed by SCAPI / OCAPI depending on the middleware configuration.
You can use this method to add new items to a basket. The added items are associated with the specified shipment. If no shipment id is specified, the added items are associated with the default shipment (called 'me').
Signature
export declare function addProductsToBasket(
props: {
basketId?: string;
items: BasketProductAddParams[];
}
): Promise<import("commerce-sdk/dist/checkout/checkout").ShopperBaskets.Basket>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
props | Required | { basketId?: string; items: BasketProductAddParams[]; } | Parameter object which can be used with this method. Refer to its type definition to learn about possible properties. |
Returns
Returns a representation of a Basket object.
Referenced Types
BasketProductAddParamsShopperBaskets.Basket
Examples
Adding a product by productId.
import { sdk } from '~/sdk.config.ts';
const items: BasketProductAddParams = [{
quantity: 1,
productId: '001',
}]
const basket = await $sdk.commerce.addProductsToBasket({
basketId: basket.value.basketId,
items,
});Adding a product by productId with options.
import { sdk } from '~/sdk.config.ts';
const items: BasketProductAddParams = [{
quantity: 1,
productId: '001',
optionItems: [{
optionId: "consoleWarranty",
optionValueId: "001",
}]
}]
const basket = await $sdk.commerce.addProductsToBasket({
basketId: basket.value.basketId,
items,
});Adding a product by productId with 'Choice of bonus product'.
import { sdk } from '~/sdk.config.ts';
const items: BasketProductAddParams = [{
quantity: 1,
productId: '001',
optionItems: [{
optionId: "consoleWarranty",
optionValueId: "001",
}]
bonusDiscountLineItemId: 'ba248414e3eee797f062162f8b'
}]
const basket = await $sdk.commerce.addProductsToBasket({
basketId: basket.value.basketId,
items,
});