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

wiltonr
Giga Contributor

I am very interested in trying this code as I have the same requirement but I'm not following your instructions.   How do I "change the CART to point to the new UI macro you created" and then I'm not sure what/how to use the rest of the code you shared.


wiltonr
Giga Contributor

I was able to figure out how to change the cart to the modified cart ui macro and the code works for Add to Cart, but if you choose Order Now it still adds what's on the screen and orders.   I'd like to have the same alert pop up when Order Now is clicked to ask the user if they are sure they meant to add another item.


adiddigi
Tera Guru

Swetha, Did this work for you? If so please mark this question answered.


andrew_lawlor
Giga Expert

You should be able to do this without modifying the OOB UI Macros. Also, if you do it from a client script onSubmit, it will also work for the 'Order Now' button. To accomplish this, you will need to define server-side code in a Script Include. You will then call this code from the client-side with GlideAjax.



Server side:


var CheckCart = Class.create();


CheckCart.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  checkCart: function() {


// Getting Item sys_id


var catItem = this.getParameter('sysparm_catItem');


var items = new GlideRecord('sc_cart_item');


  items.addQuery('cart.user', gs.getUserID());


  items.addQuery('cat_item, catItem);


  items.query();


if (items.hasNext()) {


return true;


}


return false;


});



Client side:



function checkCart() {


  var item = 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()


}


// Otherwise, allow submission.


  return true;


Thanks, I am going to try this now.   What table is the client script running on?