createCart
Method creating a new cart or restoring an anonymous cart as a user's cart in SAP Commerce Cloud.
This method sends a POST request to the createCart endpoint of the Alokai Middleware. In turn, it exchanges data with the createCart endpoint exposed by the SAP OCC API Carts Controller.
When vsf-sap-token cookie is present in the browser, 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<Res extends Cart = Cart,
Props extends CreateCartProps = CreateCartProps>(
props?: Props,
options?: MethodOptions
): Promise<Res>;Parameters
| Name | Required | Type | Description |
|---|---|---|---|
props | Optional | Props | Parameter object which can be used with this method. Refer to its type definition to learn about possible properties. |
options | Optional | MethodOptions | Options that can be passed to additionally configure the request or customize the logic in a plugin. |
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'
});