The CreatorCon Call for Content is officially open! Get started here.

How to get the count of all items in shopping cart.

heather_mcmanig
Kilo Contributor

I need to get the total counts of all the items in service catalog shopping cart to add to a custom template Here is the code I have attempted:

<g2:evaluate var="items_count" object="true">

            var items_count = 0;

            var cart = new GlideRecord('sc_cart');

            var userid = gs.getUserID();

              cart.addQuery('user', userid);

              cart.query();

            if (cart.next()) {

                      var item = new GlideRecord('sc_cart_item');

                      item.addQuery('cart', cart.sys_id);

                      item.query();

                    if(item.next()) {

                                while(item.next()){

                                          items_count =   items_count + item.quantity;

                                }                  

                      } else {

                                items_count = 0;

                      }                  

            }

  </g2:evaluate>

The problem is is that this will return 1.0. I've tried parseInt() and Math.floor as well with no luck.

I just want to get a total count of all the items in my shopping cart in service catalog.

17 REPLIES 17

Sneha Naidu
Kilo Expert

You can use the glide record function getRowCount()


SO the code will look like:        



var items_count = 0;        


var cart = new GlideRecord('sc_cart');        


var userid = gs.getUserID();            


cart.addQuery('user', userid);            


cart.query();        


if (cart.next()) {


                      var item = new GlideRecord('sc_cart_item');


                        item.addQuery('cart', cart.sys_id);


                        item.query();


                        items_count =   item.getRowCount();


                    }


That will only count the number of rows. If you want a sum of all quantities you need to use the GlideAggregate instead. Something like this would do:



var items_count = 0;    


var cart = new GlideRecord('sc_cart');    


var userid = gs.getUserID();        


cart.addQuery('user', userid);  


cart.query();    


if (cart.next()) {


  var item = new GlideAggregate('sc_cart_item');


  item.addQuery('cart', cart.sys_id);


  item.addAggregate('SUM', 'quantity');


  item.query();


  if(item.next()


      items_count = item.getAggregate('SUM', 'quantity');


}


This return nothing.


This returns 1 even with multiple different items.



                      var item = new GlideRecord('sc_cart_item');


                        item.addQuery('cart', cart.sys_id);


                        item.query();


                        items_count =   item.getRowCount();


TRY THIS



var items_count = 0;        


var cart = new GlideRecord('sc_cart');        


var userid = gs.getUserID();        


cart.addQuery('user', userid);        


cart.query();        


if (cart.next()) {


                      var item = new GlideRecord('sc_cart_item');


                      item.addQuery('cart', cart.sys_id);


                      item.query();


                  while(item.next()) {


                                  items_count = items_count+item.quantity;


                  }


}