How to get Shopping Cart Quantity in Catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2019 03:30 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2019 02:40 PM
Hi Rams,
Thank you for the update.
$('quantity').value is not working in Portal though
Regards,
Manoj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2019 04:06 PM
Is your script working in Service Portal?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-20-2020 12:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-15-2020 09:40 AM
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