How to get Shopping Cart Quantity in Catalog client script

Manoj82
Kilo Expert

Hi All,

 I am trying to get the value of shopping cart Quantity on to a catalog client script.

Requirement: Prior to Checkout, I need to identify the quantity in the cart and compare with another field "Free items" on a different table in ServiceNow instance

Based on the result, If Quantity in Cart >Free items then the user should not able to Checkout. If Freeitems >Quantity in Cart user should be able to checkout.

I have written a Catalog Client script to retrieve the values from Cart and a Script include to compare.

In the Client Script used below script Quantity value from the Shopping Cart using :
ga.addParam('sysparm_quantity', $('quantity').value); (with GlideAjax)

Even tried just an alert to check i.e. alert("('quantity').value: " + $('quantity').value); in Service portal a Java Script Error in Browser Console Error is coming. Heard "$" is an unsupported method in Portal

Can someone help me how to get Quantity value so that I can call from my Client script and compare in my Script include with my "Free Items" field

Regards,
Manoj

8 REPLIES 8

SanjivMeher
Kilo Patron
Kilo Patron

You may need to query the sc_cart_item table to get the quantity.


Please mark this response as correct or helpful if it assisted you with your question.

SatheeshKumar
Kilo Sage

use the below code to get the cart item quantity:

 

var cartId = new sn_sc.CartJS().getCartID();
var quantity =0;
var item = new GlideRecord('sc_cart_item');
item.addQuery('cart', cartId);
item.query();
while(item.next()){
	quantity=   quantity+ item.quantity;
}

gs.print(quantity);

 

 

-satheesh

Asynchronous GlideAjax does not work in onSubmit client script. Is there a way to get the quantity value in onSubmit client script through Service Portal?

Service_RNow
Mega Sage

Hi,

the helps of below thread

How to get Shopping Cart Quantity in Catalog client script ?

var count = $('quantity').value;

if(count > 1){

var conMsg = confirm('');

if(conMsg == true){

//Your code;

}

else{

return false;

}

}