Need a script to validate the current catalog item is already in the cart list

swetha53
Kilo Contributor

I have   selected a catalog item and clicked on "Add to cart" twice. catalog item was added twice to the request.In order to avoid this i need to validate on   click on                     "add to cart item" this should throw a pop-up like "Catalog item is already selected do you want to continue?"

24 REPLIES 24

I was just in a meeting with the customer.   They are wanting to know if it's possible to differentiate between which button is used.   So basically, they want to allow the use of Order Now only if the cart is empty but if they have added something to their cart already, then they want the message that tells them when they are adding the same item (which is working) but also do not want to allow the use of Order Now so the user is then forced to choose Proceed to Checkout.


Okay, I'm getting closer but not quite there.   I changed if(resp) to   if(resp=='true')   Now I don't get the comment if there is nothing in the cart, only if there is already that same item in the cart.   I get the comment on both Add to Cart and Order Now but when I choose cancel after Add to Cart it doesn't add anything (desired), but if I choose cancel after Order Now, it ends up submitting the form along with whatever is already in the cart.   So I think all that's left is to figure out how to either disable Order Now after the cart has an item in it, or to make it not submit if cancel is selected.


Andrew this is the client script that finally worked for me.   Thank you for all of your help!   🙂



function onSubmit() {



if (checkCart() == 'true'){
  var result = confirm('At least one of these items already exists in your cart - click OK if you would like to add another\nOtherwise choose cancel and Proceed to Checkout to order what is already in your cart');
  if(!result){
    return false;
  }
}
}


function checkCart() {
var catItem = gel("sysparm_id").value;
var cart = new GlideAjax('CheckCart');
cart.addParam('sysparm_name', 'checkCart');
cart.addParam('sysparm_catItem', catItem);
cart.getXMLWait();
return cart.getAnswer();
}


It is catalog_cart UI page.


vinothkumar
Tera Guru

Could you please help me on this to work on portal as well?