createCart
Create new cart or restoring an anonymous cart as a user's cart in SAP Commerce Cloud.
This method exchanges data with the createCart endpoint exposed by the SAP OCC API Carts Controller.
When vsf-sap-token cookie is present in the request, this method will create a cart for the current user. In case there is no cookie, it will create a cart for an anonymous user.
Signature
export declare function createCart(
context: SapccIntegrationContext,
props?: CreateCartProps
): Promise<Cart>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
context | Required | SapccIntegrationContext | |
props | Optional | CreateCartProps | 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.
Referenced Types
Examples
Creating a new cart.
import { sdk } from '~/sdk';
const cart = await sdk.commerce.createCart();Creating a new cart and selecting response fields.
import { sdk } from '~/sdk';
const cart = await sdk.commerce.createCart({ fields: 'code,guid,user(FULL)' });Turning an anonymous cart into the current user's cart by passing the anonymous cart guid as the oldCartId parameter.
You have to be logged-in to use this method this way.
import { sdk } from '~/sdk';
const cart = await sdk.commerce.createCart({ oldCartId: '1da746dc-aa8f-4914-a70e-49f1b32ca0db' });Merging an anonymous cart with the current user's cart by passing the anonymous cart guid as the oldCartId parameter and the user's cart guid as the toMergeCartGuid parameter.
You have to be logged-in to use this method this way.
import { sdk } from '~/sdk';
const cart = await sdk.commerce.createCart({
oldCartId: '1da746dc-aa8f-4914-a70e-49f1b32ca0db',
toMergeCartGuid: 'c2c68786-bb31-4bb4-aa1c-8ad9b43de8ef'
});