How to get the count of all items in shopping cart.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 08:49 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:04 AM
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>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:08 AM
That returns the value 1.0 for an empty cart.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2017 09:45 AM
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.