
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 09:09 AM
Hi all,
I'm trying something new (for me). I've got a UI Macro that I'm modifying.
I've added the following g:evaluate tag:
<g:evalute var="cart_count" object="true">
var cart_count = new GlideAggregate('sc_cart_item');
cart_count.addAggregate('SUM','quantity');
cart_count.addQuery('active','true');
cart_count.addQuery('sys_created_by',gs.getUserName());
cart_count.setGroup(false);
cart_count.query();
cart_count;
</g:evaluate>
The above code will give me a quantity for the number of catalog items I have in a shopping cart for the current user. This works great.
Later in the code, I have this <j:if> statement:
<j:if test="${cart_count.next()}">${cart_count.getAggregate('SUM','quantity')}</j:if>
The above code works great if all I want to show is how many items a user has in his/her cart.
What I'd like to do is throw an if statement that checks if the cart quantity for a user is 0:
if (SUM == 0/null) {
display '0'
}
else {
${cart_count.getAggregate('SUM','quantity')}
}
Obviously I have the 2nd half working, just not sure how to do the 1st half. Any help is greatly appreciated-
Matthew
Solved! Go to Solution.
- Labels:
-
User Interface (UI)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 11:23 AM
This worked for me:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate jelly="true">
var cart_count = new GlideAggregate('sc_cart_item');
cart_count.addAggregate('SUM','quantity');
cart_count.addQuery('active','true');
cart_count.addQuery('sys_created_by',gs.getUserName());
cart_count.setGroup(false);
cart_count.query();
</g:evaluate>
<j:if test="${cart_count.next()}">
<j:choose>
<j:when test="${cart_count.getAggregate('SUM','quantity')} != 0">
Number of items: ${cart_count.getAggregate('SUM','quantity')}
</j:when>
<j:otherwise>
Number of items: 0
</j:otherwise>
</j:choose>
</j:if>
</j:jelly>
3 sc_cart_items:
After deleting the items:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 10:25 AM
You have to use j:choose and j:when...
<j:choose>
<j:when test="${cart_count.getAggregate('SUM','quantity')} == 0 || ${cart_count.getAggregate('SUM','quantity')} == null}">
0
</j:when>
<j:otherwise>
${cart_count.getAggregate('SUM','quantity')}
</j:otherwise>
</j:choose>
There is no if/else in jelly.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 10:50 AM
Mike
Thanks for the reply. I figured I'd have to use a combination of choose/when tags.
Here is my code; when I run the page w/ the macro, I get zippy/nada/nuttin':
- <g:evaluate var="cart_count" object="true">
- var cart_count = new GlideAggregate('sc_cart_item');
- cart_count.addAggregate('SUM','quantity');
- cart_count.addQuery('active','true');
- cart_count.addQuery('sys_created_by',gs.getUserName());
- cart_count.setGroup(false);
- cart_count.query();
- cart_count;
- </g:evaluate>
- <j:choose>
- <j:when test="${cart_count.getAggregate('SUM','quantity')} == 0 || ${cart_count.getAggregate('SUM','quantity')} == null}">
- Number of items: 0
- </j:when>
- <j:otherwise>
- Number of items: ${cart_count.getAggregate('SUM','quantity')}
- </j:otherwise>
- </j:choose>
Here is a screenshot:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 10:55 AM
Try this:
<g:evaluate jelly="true">
var cart_count = new GlideAggregate('sc_cart_item');
cart_count.addAggregate('SUM','quantity');
cart_count.addQuery('active','true');
cart_count.addQuery('sys_created_by',gs.getUserName());
cart_count.setGroup(false);
cart_count.query();
</g:evaluate>
<j:if test="${cart_count.next()}">
<j:choose>
<j:when test="${cart_count.getAggregate('SUM','quantity')} == 0">
Number of items: 0
</j:when>
<j:otherwise>
Number of items: ${cart_count.getAggregate('SUM','quantity')}
</j:otherwise>
</j:choose>
</j:if>

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 11:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2015 11:23 AM
This worked for me:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate jelly="true">
var cart_count = new GlideAggregate('sc_cart_item');
cart_count.addAggregate('SUM','quantity');
cart_count.addQuery('active','true');
cart_count.addQuery('sys_created_by',gs.getUserName());
cart_count.setGroup(false);
cart_count.query();
</g:evaluate>
<j:if test="${cart_count.next()}">
<j:choose>
<j:when test="${cart_count.getAggregate('SUM','quantity')} != 0">
Number of items: ${cart_count.getAggregate('SUM','quantity')}
</j:when>
<j:otherwise>
Number of items: 0
</j:otherwise>
</j:choose>
</j:if>
</j:jelly>
3 sc_cart_items:
After deleting the items: