Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

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

Hi Rams,

Thank you for the update.

$('quantity').value is not working in Portal though

Regards,
Manoj

Dharti2
Kilo Contributor

Is your script working in Service Portal?

Preethi26
Tera Contributor

Same problem for me now. I have to compare the cart item quantity with my custom table created in studio.

example , if  the quantity field in custom table(items table) is 2 and i am ordering in SC as 3 it should not checkout.

but its not working plz help me with the script.

Regards,

Preethi

Hi Preethi,

 

I was able to fix this and is working fine for me as intended.

 

Do the following in "SC Shopping Cart" widget


Server script: 

var userID = gs.getUser().getID();
//var cart = new SPCart(input.cartName, userID);
var cart = new SPCart_Custom(input.cartName, userID);
data.Flag = false;
data.quantities = {};
data.quantity = 0;
data.FIL = 0;

var gr = new GlideRecord('custom table');
if(gr.get('u_user', gs.getUserID())){
data.FIL = gr.getValue('custom field');
}
if (input && input.action === "error") {
data.quantity = input.tempQuantity;
data.quantities = JSON.parse(JSON.stringify(input.tempQuantities));
data.Flag = data.Flag;
gs.addErrorMessage('You have exceeded your ordering limit');
return;


Client script: AT checkout

c.triggerCheckout = function($evt, twostepCheckout) {
if(c.data.Flag && c.data.FIL >= 0 && c.data.quantity > c.data.FIL)
{
c.data.tempQuantities = JSON.parse(JSON.stringify(c.data.quantities));
c.data.tempQuantity = c.data.quantity;
c.data.tempFlag = c.data.Flag;
c.data.action = 'error';
c.server.update().then(function(response) {
c.data.action = null;
});

Regards,
Manoj