Can we fetch the data of shopping cart items and their variables before checkout

Mohammed Lais1
Mega Guru

in shopping cart   page, before checking out can we get the details of items present in the cart and their variables in script.

1 ACCEPTED SOLUTION

Rakesh Mamidi
ServiceNow Employee
ServiceNow Employee

If you need to do Glide record, here is the sample code.




var sc = new GlideRecord('sc_cart');


sc.addQuery('user',gs.getUserID());


sc.query();


if(sc.next()){


var item = new GlideRecord('sc_cart_item');


item.addQuery('cart',sc.sys_id);


item.query();


while(item.next()){


    var options = new GlideRecord('sc_item_option');


    options.addQuery('cart_item',item.sys_id);


    options.query();


    while(options.next()){


        gs.print("Question:" options.item_option_new.getDisplayValue());  


          gs.print("Question:" options.value.getDisplayValue());        


        }


  }


}


View solution in original post

8 REPLIES 8

Rakesh Mamidi
ServiceNow Employee
ServiceNow Employee

If you need to do Glide record, here is the sample code.




var sc = new GlideRecord('sc_cart');


sc.addQuery('user',gs.getUserID());


sc.query();


if(sc.next()){


var item = new GlideRecord('sc_cart_item');


item.addQuery('cart',sc.sys_id);


item.query();


while(item.next()){


    var options = new GlideRecord('sc_item_option');


    options.addQuery('cart_item',item.sys_id);


    options.query();


    while(options.next()){


        gs.print("Question:" options.item_option_new.getDisplayValue());  


          gs.print("Question:" options.value.getDisplayValue());        


        }


  }


}


Thanks a lot rakesh


Hi @Rakesh Mamidi,

I tried your solution and it worked for me but now I'm having a problem when I tried it on another instance. I'm seeing two carts for my User.

find_real_file.png

On my development instance, I have two carts too. But for some reason, the first that's being returned on the query is the DEFAULT one, where my item is, so everything's good. But on my test instance, the one that's being returned is the saved_items, that's why no item is being returned.

Do you know the difference between these two carts and how an item is being assigned to the carts?

Aslo, do you think there will be no impact if I just use

while(sc.next()){
....
}

instead of

if(sc.next()){
....
}

so I will be able to go through all the carts?

Thanks!

Sameer18
Tera Contributor

There is a shorter way present in OOTB Cart API (Not really sure  in which version this was introduced).  This is simpler than querying records from two table.

I hope this will fulfill your requirement.

 

Example 1: Get the details of individual Item:-

var cartItemID = "1a1ca92d1be93b004bcb98efbd4bcb07";//Sys_id of cart item
var cartJS = new sn_sc.CartJS("saved_items"); //pass the cart name
return cartJS.getCartItemDetails(cartItemID); 

Output JSON format:

{
  "result": {
    "updated_on": "2019-05-20 09:43:19",
    "catalog_item_id": "04b7e94b4f7b4200086eeed18110c7fd",
    "short_description": "Lenovo - Carbon x1",
    "recurring_subtotal_price": "₨6,969.1784",
    "subtotal_price_display": "₨76,660.9623",
    "delivery_time": "5 Days",
    "sys_class_name": "sc_cat_item",
    "sys_id": "c500755d4fa13300fc7ef3117310c7da",
    "price": "₨76,660.9623",
    "recurring_frequency": "Annually",
    "price_display": "₨76,660.9623",
    "subtotal_price": "₨76,660.9623",
    "variables": {
      "Adobe Photoshop": "",
      "Adobe Acrobat": "",
      "Optional Software": "",
      "Additional software requirements": ""
    },
    "quantity": "1",
    "recurring_subtotal_price_display": "₨6,969.1784",
    "item_id": "04b7e94b4f7b4200086eeed18110c7fd",
    "recurring_price": "₨6,969.1784",
    "show_price": true,
    "item_name": "Standard Laptop",
    "cart_item_id": "c500755d4fa13300fc7ef3117310c7da",
    "recurring_subtotal": 6969.1784,
    "no_delivery_time": false,
    "show_quantity": true,
    "created_by": "admin",
    "picture": "b28db9444f889200086eeed18110c724.iix",
    "created_on": "2019-05-20 09:43:19",
    "localized_price": "₨76,660.962",
    "subtotal": 76660.9623,
    "show_recurring_price": true,
    "name": "Standard Laptop",
    "updated_by": "admin",
    "localized_recurring_price": "₨6,969.178",
    "recurring_price_display": "₨6,969.1784",
    "order_guide": ""
  }
}

 

Example 2: Get Details of entire cart

 

var cart = new sn_sc.CartJS("saved_items"); // pass the cast name, in this case wishlist
return cart.checkoutSummary(true);

 Output JSON format: 

{
  "result": {
    "cart_id": "6defe15d4fa13300fc7ef3117310c70a",
    "items": [
      {
        "updated_on": "2019-05-20 09:43:19",
        "catalog_item_id": "04b7e94b4f7b4200086eeed18110c7fd",
        "short_description": "Lenovo - Carbon x1",
        "recurring_subtotal_price": "₨6,969.1784",
        "subtotal_price_display": "₨76,660.9623",
        "delivery_time": "5 Days",
        "sys_class_name": "sc_cat_item",
        "sys_id": "c500755d4fa13300fc7ef3117310c7da",
        "price": "₨76,660.9623",
        "recurring_frequency": "Annually",
        "price_display": "₨76,660.9623",
        "subtotal_price": "₨76,660.9623",
        "variables": {
          "Adobe Photoshop": "",
          "Adobe Acrobat": "",
          "Optional Software": "",
          "Additional software requirements": ""
        },
        "quantity": "1",
        "recurring_subtotal_price_display": "₨6,969.1784",
        "item_id": "04b7e94b4f7b4200086eeed18110c7fd",
        "recurring_price": "₨6,969.1784",
        "show_price": true,
        "item_name": "Standard Laptop",
        "cart_item_id": "c500755d4fa13300fc7ef3117310c7da",
        "recurring_subtotal": 6969.1784,
        "no_delivery_time": false,
        "show_quantity": true,
        "created_by": "admin",
        "picture": "b28db9444f889200086eeed18110c724.iix",
        "created_on": "2019-05-20 09:43:19",
        "localized_price": "₨76,660.962",
        "subtotal": 76660.9623,
        "show_recurring_price": true,
        "name": "Standard Laptop",
        "updated_by": "admin",
        "localized_recurring_price": "₨6,969.178",
        "recurring_price_display": "₨6,969.1784",
        "order_guide": ""
      },