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

lasse3
Mega Guru

In the line items_count =   items_count + item.quantity please verify that you are actually adding an int to an int.



There is no need for your "if / else" statement. You can simply do the while loop, as this will only execute if there are results in the item.next().



Try this:



<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();


                    while(item.next())


                          items_count =   items_count + parseInt(item.quantity);            


            }


  </g2:evaluate>


That returns the value 1.0 for an empty cart.


I am so sorry. I totally missed that the quantity field on the sc_cart_item is per default a choice list, so I believe that you need to call item.quanity.getDisplayValue(). Try this out:



<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();


                    while(item.next())


                          items_count =   items_count + parseInt(item.quantity.getDisplayValue());          


            }


  </g2:evaluate>



Hope this helps


            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 + parseInt(item.quantity.getDisplayValue());          


            }



Still return 1.0 for an empty cart or one with items.