SDK Example: Add a Child Product to the Basket (Fall ‘20)
Use the example SDK code in this topic to add a child product to a basket.
1// Instantiate the input object for AddToCartInput to specify parameters
2const input = digitalCommerce.createAddToCartInput();
3input.catalogCode = "Mobiles"; // use your Catalog Code
4input.basketAction = "AddChildProduct"; // deleting an offer that was added to cart
5input.basketKey = "01tf2000007Rb9JAAS"; // use your basketKey
6input.actionParams = {
7 rootProductCode: "iPhoneX", // use your rootProductCode
8 parentLineKey: "c3a4590a2d231e7022e04e5eba539ead", // use your parentLineKey
9 parentHierarchyPath: "01tf2000007Rb9JAAS", // use your parentHierarchyPath
10 offer: "DeclineEquipmentProtection", // use your offer_code
11 cartContextkey: "d80b2927f07938aefdc7e2e349e2ede7", // use your cartContextKey
12 bundleContextKey: "c3a4590a2d231e7022e04e5eba539ead" // use your bundleContextKey};
13input.customFields = ["customAttribute__c"]; // optional field which returns custom fields for given keys
14
15// Invoke Basket API via method addToBasket()digitalCommerce
16 .addToCart(input)
17 .then(response => {
18 Logger.info(
19 "Vlocity digital commerce addToCart with basketAction=AddChildProduct for anonymous user: " +
20 JSON.stringify(response)
21 );
22 })
23 .catch(error => {
24 Logger.info(
25 "addToBasket with basketAction=AddChildProduct for anonymous user rest call failed" +
26 error
27 );
28 });
Possible error conditions:
“AddToCartInput::getAPIPath() must have catalogCode.”
“AddToCartInput::getAnonymousSiteRestUrl() must have catalogCode.”
“AddToCartInput::getRequestPayload() must have basketAction.”
“AddToCartInput::getRequestPayload() must have rootProductCode when basketAction is ‘AddChildProduct’.”
“AddToCartInput::getRequestPayload() must have parentLineKey when basketAction is ‘AddChildProduct’.”
“AddToCartInput::getRequestPayload() must have parentHierarchyPath when basketAction is ‘AddChildProduct’.”
“AddToCartInput::getRequestPayload() must have offer when basketAction is ‘AddChildProduct’.”
“AddToCartInput::getRequestPayload() must have cartContextkey when basketAction is ‘AddChildProduct’.”
“AddToCartInput::getRequestPayload() must have bundleContextKey when basketAction is ‘AddChildProduct’.”