createCart(cartInput)

Creates a cart for a buyer in a webstore.

Syntax 

1import { createCart } from 'lightning/commerceCartApi';
2createCart(cartInput: object): Promise<CartSummary>

Commerce API Resource 

1POST /commerce/webstores/{webstoreId}/carts

createCart uses this Commerce API resource.

Parameters 

  • webstoreId—(Required) The ID of the webstore. The ID prefix is 0ZE.
  • version—(Required) The API version for the adapter, for example, v62.0. Without this parameter, the call doesn’t fire.
  • cart—(Required) The cart input object. Accepts these fields:
    • effectiveAccountId—(Optional) The effective account ID for the cart.
    • name—(Optional) The friendly name for the cart.
    • type—(Optional) The cart type. Valid values: Cart, PayNowReadOnly, ReadOnly, Template.
    • currencyIsoCode—(Optional) The ISO currency code for the cart, for example, USD.

Additional fields (orderOwnerId, isSecondary, description, typeAsString, customFields) are available in API version 64.0 and later.

Note

Returns 

A Promise that resolves with a CartSummary object with these fields:

  • cartId—The ID of the created cart.
  • name—The name of the cart.
  • status—The status of the cart, for example, Active.
  • type—The cart type.
  • webStoreId—The ID of the webstore associated with the cart.

Usage 

createCart is an imperative function, not a @wire adapter. Call it in response to a user action.

1import { LightningElement } from 'lwc';
2import { createCart } from 'lightning/commerceCartApi';
3
4export default class CreateCartExample extends LightningElement {
5  async handleCreateCart() {
6    try {
7      const result = await createCart({
8        webstoreId: '0ZExx0000000001',
9        version: 'v62.0',
10        cart: {
11          name: 'My Cart',
12          currencyIsoCode: 'USD',
13        },
14      });
15      console.log('Cart created:', result.cartId);
16    } catch (error) {
17      console.error('Error creating cart:', error);
18    }
19  }
20}

Error Handling 

If the call fails, the Promise rejects with an error object containing an errorCode and a message.

See Also