Add Multiple items to cart using CartJS

Community Alums
Not applicable

I am attempting to use the CartJS API to add multiple items to a cart and checkout. Here is the code I have thus far:

var cart = new sn_sc.CartJS();
var request = {
    'special_instructions': 'Test',
    'requested_for': 'd7abd25a3b300300d901655593efc407',
    'delivery_address': "111 Main St",
};
var item1 = {
    'sysparm_id': 'a46240dddb631300818873ffbf961938',
    'variables': {
        'resource': 'McNuggets',
    }
};
var item2 = {
    'sysparm_id': 'f613e9a7db130300818873ffbf9619cd',
    'variables': {
        'test_text': 'McRib',
    }
};
cart.addToCart(item1);
cart.addToCart(item2);
cart.submitOrder(request);

While this does execute and create the REQ, it only adds the first RITM. I cannot add multiple items. Is there another way to go about this to add multiple items to the request?

Using the older 'Cart' API, I can successfully add multiple items to the cart and it creates the REQ and RITMs as expected:

var aItems = ['McNugget', 'McRib', 'McMuffin'];
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var catItem = 'a46240dddb631300818873ffbf961938';
aItems.forEach(function (item) {
    cart.addItem(catItem);
    cart.setVariable(catItem, 'resource', item);
});
var rc = cart.placeOrder();

Results in a REQ with three RITMs. Perhaps that is the better way to go about this? I was hoping to use the newer CartJS API, as it seems a little more robust.

Cheers,

Tim

7 REPLIES 7

Sure

 

For testing i use a UI action that is executed serverside. 

it fetches some required data orders x number of items.

I use a script include to handle the ordering and to resolve some data i need to pass to the ordered items. 

try
{
    var util = new u_sp_AccessUtil();

    //get profiles from user
    var jps = "acac9947dbde5740c3567b9dae961928";

    var jobProfiles = util.GetJobProfiles(jps);

    //get accessprofiles from jobprofiles
    var AccessProfiles = util.ResolveJobProfiles(jobProfiles);

    var response = util.AssignAccessProfiles(jps, AccessProfiles);

    gs.info("receipt" + JSON.stringify(response));
}
catch (e)
{
    gs.error(e);
} 

 

 

the function that creates order items and checkout the cart.

    //Assigns Access profiles by reading and populating catalog item 
    AssignAccessProfiles: function (UserSysId, arrayAccessProfileIds)
    {
        try
        {
            if (this.IsArray(arrayAccessProfileIds))
            {
                var cart = new sn_sc.CartJS();
                var grAccessProfile = this.getGlideRecordAccessProfile();

                for (var index in arrayAccessProfileIds)
                {
                    var profileId = arrayAccessProfileIds[index];

                    grAccessProfile.get(profileId);

                    var provItem = grAccessProfile.getValue("u_sp_provision_catalog_item");

                    if (provItem)
                    {
                        var type = grAccessProfile.getValue('u_sp_type');

                        var order;
                        if (type === "adgroup")
                        {
							//type: list
                            var adgroups = grAccessProfile.getValue("u_sp_ad_groups");

							order = {};
							var orderVariables = {};
							
							//variable type = reference
							orderVariables.u_sp_get_user_sys_id = UserSysId;
							//variable type = list
							orderVariables.u_sp_ad_groups = adgroups;
							
							order.sysparm_id = provItem;
							order.sysparm_quantity = "1";
							order.variables = orderVariables;
                        }
                        //add additonal types later.
						
                        if (order)
                        {
							gs.info("adding Item " + JSON.stringify(order));
							//ONLY one item i added to cart?
							//order updates properly
							//cart does not update second time. 
                            var c = cart.addToCart(order);
							
							//just for the breakpoint
							var e = c;
                        }
                    }
                    else 
                    {
                        gs.warn(grAccessProfile.getValue('name') + " is missing a provision Item");
                    }
                }
				
				//log for debugging
				var itemsCart = cart.getCartItems();
				gs.log("itemsFromCart " + itemsCart.getRowCount());
				
                var d = cart.checkoutCart();

                return d;
            }
        }
        catch (er)
        {
            gs.error(er);
        }
}

 

even though the addToCart() have been invoked twice with different order objects the requests is only added with one ritm:

find_real_file.png

 

 

reverted to the old cart api

Jeremy J
Giga Contributor

After struggling with this for a bit, I've confirmed this is likely due to a current issue with addToCart. I confirmed that if I called sn_sc.CartJS() before every add, it worked exactly as expected. However, without doing so, only the first item is ever added to the cart. The KB above indicates only London, but I'm seeing the exact same behavior on Kingston.