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

Try this


Previously, I just had a comment snippet in there instructing you what to code in that place. This script is updated with the actual code.



function onSubmit() {


checkCart() ;



function checkCart() {
//alert('inside function');


  var catItem = gel("sysparm_id").value;


  var cart = new GlideAjax('CheckCart');


  cart.addParam('sysparm_name', 'checkCart');


cart.addParam('sysparm_catItem', catItem);


  cart.getXMLWait();


  var resp = cart.getAnswer();


  if (resp) {


// prompt user with confirm()


confirm('This item already exists in your cart - would you like to order another?');


}


// Otherwise, allow submission.


  return true;


}
}


that should be:


return confirm('This item already exists in your cart - would you like to order another?');


Sorry - I did it quick and didn't put my own comment in there   🙂   Very close but this time I get the comment even if there isn't already an item in the cart - i get it on ordernow and add to cart before I have one item there.


What value are you getting back from the script include? Can you print your resp variable? I wrote all of that really quickly as an example, so this will just confirm that it's working. Go ahead and


alert(resp);


on the line before the confirm.


Returns false