About sn_sc.CartJS()

zhangyuchen
Tera Contributor

Hello All,

Regarding the method "var cart = new sn_sc.CartJS();", if it is used twice within a short period by the same user, the same cart is returned.

Is there a way to ensure a different cart is created each time it is used?

Thanks & Regards

3 REPLIES 3

Abhay Kumar1
Giga Sage

@zhangyuchen When you use var cart = new sn_sc.CartJS();, it returns the user's current cart if one already exists. This behavior is by design, as ServiceNow treats the cart as a session-based object, where each user typically has one active cart.

Issue:

If you call new sn_sc.CartJS() multiple times within the same session (or in a short period), it refers to the same cart unless the cart is checked out or explicitly cleared.

Solution: Create a New Cart

to force the creation of a new cart each time, you need to manually clear the existing cart or work with a unique cart identifier.

Hope this will help you.

Thank you for your response!

How can i use a unique cart identifier?

var cart = new sn_sc.CartJS("unique_cart_identifier");
cart.setRequestedFor(gs.getUserID());
 var item = {
     'sysparm_id': '4d1a15d783542210c92d9496feaad3ea',
     'sysparm_quantity': '1',
     'variables': {
   
     }
 };
 var cartDetails = cart.addToCart(item);
 var checkoutInfo = cart.checkoutCart();