is there a way to automatically clear the cart when an order guide is first opened?

patricklatella
Mega Sage

I'm looking for a way to have an automatic cache out and empty of the cart when an order guide is opened to eliminate the possibility of old or "unchecked out" items being in the cart when the "checkout" page within the Order Guide is reached.

is this possible?     thanks!

1 ACCEPTED SOLUTION

Patrick, here you go. please mark as correct. also, check impersonating a user with no role.



onLoad client script:



function onLoad() {


    //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('CartUtil');


  ga.addParam('sysparm_name', 'clearCart');


  ga.getXMLWait();


}



script include:




var CartUtil = Class.create();


CartUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {


clearCart: function() {


var clear = new GlideRecord('sc_cart_item');


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


clear.addQuery('active', 'true');


clear.query();


clear.deleteMultiple();


  return;


  }


});


View solution in original post

20 REPLIES 20

patricklatella
Mega Sage

find_real_file.png


so with the box checked the cart above was my first order guide getting to the "Checkout" page.   Then if I navigate away without submitting (which a user might do), then when I open the order guide again the next time, the items from the first cart are still there.



find_real_file.png


we are not using the 2 step check out model.   Is that necessary to get this to work?   We use a custom app store instead of the shopping cart.   So we just need the cart to empty when the order guide is opened because of this one scenario.  


patricklatella
Mega Sage

and this is the result with the 2 step checkout set to true, and the "



patricklatella
Mega Sage

from that other thread, I've pulled a couple things...definitely not working, but wonder if a simple edit is all that's needed?   With this client script and script include the cart clears, but not til after the user moved to "Choose Options", so you get this.   Need it to clear when the "Describe Needs" page first loads so that when the user moves to the "Choose Options" page ONLY the new items from the current order guide are there.   See my scripts below...see anything?   thanks



find_real_file.png


client script:



function onLoad() {  


    //Type appropriate comment here, and begin script below  


    var ga = new GlideAjax('CartUtil');  


  ga.addParam('sysparm_name', 'clearCart');


  ga.getXMLWait();  


}  



script include:



var CartUtil = Class.create();  


CartUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


  clearCart: function() {  


  var cart = new GlideappCart();  


  cart.get(null, gs.getUserID()).empty();  


  return;  


  }  


});


try this in your script include. there may be a better way which i dont knw.



var clear = new GlideRecord('sc_cart_item');


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


clear.addQuery('active', 'true');


clear.query();


clear.deleteMultiple();


}


return;


patricklatella
Mega Sage

excellent Ajit, thanks works!   here are my final scripts: (copy and paste this into one more reply from you and I'll mark your reply as the correct answer).   thanks!



onLoad client script:



function onLoad() {


    //Type appropriate comment here, and begin script below


    var ga = new GlideAjax('CartUtil');


  ga.addParam('sysparm_name', 'clearCart');


  ga.getXMLWait();


}



script include:



var clear = new GlideRecord('sc_cart_item');


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


clear.addQuery('active', 'true');


clear.query();


while(clear.next()){


clear.deleteRecord();


}