Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.
Create a Cart and Cart Item with Custom Fields in a Commerce Store
Custom fields are optional and must be previously defined for the WebCart and CartItem sObjects. See Create Custom Fields. Field-level security rules from the shopper profile are applied to the WebCart and CartItem custom fields. The rules are applied for registered shoppers and for the guest shopper profile.
To create a cart with custom fields, call createCart(webstoreId, cartInput). Specify your custom fields using the customFields property of cartInput. The type for customFields is List<SObject>, where the sObject is a WebCart.
Then, to add an item to the cart, call addItemToCart(webstoreId, effectiveAccountId, activeCartOrId, cartItemInput, currencyIsoCode). You can specify custom fields using the customFields property of cartItemInput. Again, the type of customFields is List<SObject>, but the sObject must be a CartItem.
In this scenario we assume that further customization sets a custom field within the Cart Calculate API flow onto the cart item for further use.
1ID webStoreId = '0ZEOL000000063r4AA';
2 ID accountId = '001OL000002LC0qYAG';
3 ID productId = '01tOL000000ETzuYAG';
4
5 List<SObject> webCartList = new List<SObject>();
6 WebCart webCart = new WebCart();
7 webCart.webCartCustomTextField__c = 'webCartCustomFieldValue';
8 webCartList.add(webCart);
9
10 final ConnectApi.CartInput cartInput = new ConnectApi.CartInput();
11 cartInput.effectiveAccountId = accountId;
12 cartInput.name = 'Cart With Custom Fields';
13 cartInput.customFields = webCartList;
14
15 // create a cart
16 ConnectApi.CartSummary cartSummary = ConnectApi.CommerceCart.createCart(webStoreId, cartInput);
17
18 ID cartId = cartSummary.cartId;
19
20 // Given
21 List<SObject> cartItemList = new List<SObject>();
22 CartItem cartItem = new CartItem();
23 cartItem.cartItemCustomNumberField__c = 12.34;
24 cartItemList.add(cartItem);
25
26 final ConnectApi.CartItemInput input = new ConnectApi.CartItemInput();
27 input.productId = productId;
28 input.quantity = '2';
29 input.type = ConnectApi.CartItemType.Product;
30 input.customFields = cartItemList;
31
32 // add an item to the previously created cart
33 ConnectApi.CartItem itemResult = ConnectApi.CommerceCart.addItemToCart(webStoreId, accountId, cartId, input, 'USD');
34
35 // response contains all (accessible) custom fields for which data was set
36 CartItem cartItemResult = (CartItem)itemResult.customFields[0];
37 // the value from request (if not changed during flow)
38 Double valueFromRequest = cartItemResult.cartItemCustomNumberField__c;
39 // an additional customization value, e.g. set by the cart calculation flow
40 String valueForCustomization = cartItemResult.additionalCustomField__c;