How to add to a current cart instead of new cart()

e_wilber
Tera Guru

I have a script that works great- it copies a previous request and tosses all the RITMs into the curent user's shopping cart. The problem is, the script is deleting everything in their cart with new cart = Cart();

I found some code on the wiki (below) but it doesn't work. In fact it stops my script from copying anything to the cart at all. I did a test print on the userID and it is finding my userID and my cart, it just doesn't add items to it.

var cart = getCart();

  function getCart() {

          var cart = new GlideRecord('sc_cart');

          var userid = gs.getUserID();

          cart.addQuery('user', userid);

          cart.query();

          if (cart.next()) {

                  // we already have a cart all is well

    gs.log('found cart ' + userid);

          }

          else {        

    gs.log('new cart');

                  cart.initialize();

                  cart.user = userid;

                  cart.insert();

          }

      return cart;

  }

Below is the actual code I'm trying to use. Anyone see why I'm not able to keep the items in my shopping cart?

var origReq = new GlideRecord('sc_request');  
origReq.addQuery('sys_id', rid);  
origReq.query();  

if(origReq.next()){  
        var cart = getCart();
gs.log(cart);
cart.cart.update();  
//cart.cart.special_instructions = 'PARENT:' + parentRequest.sys_id;  
//cart.cart.update();  

           

var ritm = new GlideRecord('sc_req_item');
ritm.addQuery('request', reqID);
ritm.query();
while (ritm.next()) {
// start copying the RITM vars
var oldRITM   = new GlideRecord('sc_req_item');  
oldRITM.get(ritm.sys_id);
var newRITM = cart.addItem(oldRITM.cat_item, 1);  
for (var varName in oldRITM.variables) {  
cart.setVariable(newRITM, varName, oldRITM.variables[varName]+'');  
}  
}
}  

}

15 REPLIES 15

Hi British,



Or you could allow me to follow you or you be a follower of me. Then we could private message.


chw
Kilo Contributor

Unfortunately I'm having quite a hard time to figure out how to do that. On the other hand it might also be useful to others if we start a discussion or discuss related matters within the thread.


British,



If you look in your Community inbox you should see a request from me. You'll need to accept that in order for me to message you.


chw
Kilo Contributor

Hi Eric,



the behaviour you are experiencing lies in the constructor of the Cart class itself. The initialize function contains a call to this.clearCart(). So if you instantiate (e.g new Cart("Default", gs.getUserID())) you will get the cart you are looking for, current users default cart, but it has been emtied by the constructor call.


One possible solution would be to inherit from the Cart class and override the initialize function, leaving out the call to this.clearCart(). I tried tried it out on my DEV instance and it works fine.


The advantage would be that you have access to all the convenient methods of Cart(check out the Cart script include) including the abitlity to add your own code or override more existing functionality. The drawback here is that on upgrading your system you always need to recheck if SNOW changed the implementation of the Cart class that you inherit from.



This is what the code of my extending class looks like:



var CustomCart = Class.create();


CustomCart.prototype = Object.extendsObject(Cart, {



      initialize : function (cartName, userID) {


      this.cartName = !cartName ? null : cartName;


      this.userID = !userID ? null : userID;


      this.cart = this.getCart();



},


     


      type: 'CustomCart'


});



It in a background script on your developer instance to see if it fits your use case:



var c = new CusomtCart("DEFAULT", gs.getUserID());


c.addItem(<sys_id_of_some_cat_item>);



I kept adding iPhones to my custom cart like this from a background script.



I hope I could help a little



Regards



Chris


Wonderful, exactly what I was looking for