Issue
This article shows the use of the addToCart()
function to add multiple items into the current user's cart.
It is working as expected to call sn_sc.CartJS()
multiple times in the script to be able to add multiple items.
Procedure
- Log in to any out-of-box instance as System Administrator (Or any user with admin role).
- Go to System Definition > Script - Background. And test the following script:
//************************************************************ var cart = new sn_sc.CartJS(); for (i = 0; i < 2; i++) { gs.addInfoMessage('i: '+ i); var item = { 'sysparm_id': '10a2f5dfc6112276018db58138c7a1e0', //sys_id of catalog item 'sysparm_quantity': '1', 'variables': { }, }; cart.addToCart(item); } //************************************************************
- We are expecting two items to be added to the user's cart, but if you check the sc_cart record for the current user, only one item got added.
- (To find current user's sc_cart records, type sc_cart.list in the instance menu Filter navigator.
- Filter the resulting list on the User column.)
- Delete all the sc_cart records for the current user.
- Test the following script in System Definition > Script - Background again:
//************************************************************ for (i = 0; i < 2; i++) { gs.addInfoMessage('i: '+ i); var cart = new sn_sc.CartJS(); var item = { 'sysparm_id': '10a2f5dfc6112276018db58138c7a1e0', 'sysparm_quantity': '1', 'variables': { }, }; cart.addToCart(item); } //************************************************************
Note "var cart = new sn_sc.CartJS();" is inside the loop now. This script adds two items into the cart.
Release
London , Orlando
Related Links
- CartJS (Developer docs)