You are reading the documentation for v7.x and earlier, which is no longer the latest version. Switch to v8.x
addToCart
Add product to the cart in SAP Commerce Cloud.
This method exchanges data with the createCartEntry endpoint exposed by the SAP OCC API Cart Entries Controller.
When vsf-sap-token cookie is present in the request, this method will add a product to the current user's cart. In case there is no cookie, it will add it to the anonymous user's cart. For logged-in users, make sure to pass cart code as the cartId param. In case of anonymous users, pass cart guid instead.
Signature
export declare function addToCart(
context: SapccIntegrationContext,
props: AddToCartProps
): Promise<CartModification>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | SapccIntegrationContext | |
props | Required | AddToCartProps | 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 Cart Modification.
Referenced Types
Examples
Adding product to the cart.
import { sdk } from '~/sdk';
const cartModification = await sdk.commerce.addToCart({
cartId: '00035084',
entry: {
quantity: 1,
product: { code: '1992695' }
}
});Adding product to the cart and selecting response fields.
import { sdk } from '~/sdk';
const cartModification = await sdk.commerce.addToCart({
cartId: '00035084',
entry: {
quantity: 1,
product: { code: '1992695' }
},
fields: 'quantity,entry(product(code))'
});Adding product to the cart and setting deliveryPointOfService.
import { sdk } from '~/sdk';
const cartModification = await sdk.commerce.addToCart({
cartId: '00035084',
entry: {
quantity: 1,
product: { code: '1992695' },
deliveryPointOfService: { name: 'ap_warehouse_e' }
},
});