Limit items in cart to 20 items only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2025 10:45 AM
Hi All,
I am working on a requirement to limit the number of items that can be added to a cart is 20.
If user adds 21 item then an error msg , or info msg or an alert to notifiy user that max number of items allowed in cart is only 20.
Wrote Before Insert Business rule for the same but there are some issues i am facing
for testing purpose i am using 3 as limit.
(function executeRule(current, previous /*null when async*/ ) {
var cartItems = new GlideRecord('sc_cart_item');
cartItems.addQuery('cart', current.cart);
cartItems.query();
var itemCount = cartItems.getRowCount();
if (itemCount >= 3) {
gs.addErrorMessage('You can only add upto 3 items to your cart');
current.setAbortAction(true);
}
})(current, previous);
BR works fine, it's not allowing more than 3 items, when i add 4th it's throwing error msg but error msg is showing up on second click on 'Add to Cart' button , there is some unknown delay.
and another issue i am facing is when i do Proceed to check out with just 3 items , it's checking out fine but the error msg is showing up in order status page as shown below.
this error after checkout appears only when i tried to add 4th item using 'Add to cart' button on main request page and throws error on 2nd click and when i click proceed to check with 3 items then this error msg shows up on order status page though i submitted 3 items only.
If i simply order only 3 items and click Proceed to check out then that error is not appearing on checkout page , works smooth if i don't try to add 4th item and just submit with 3 items.
Please throw in your advise on how to tackle this issue.
I really appreciate your time reading this post towards the end. Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 06:25 AM
Here's something we use. It's not perfect, but workable. In place of the Business Rule, add this client script to a variable set that is included in every catalog item, or create a new (empty) variable set to hold this script and add it to every catalog item
function onLoad() {
document.getElementById('oi_add_to_cart_button').addEventListener("click", function(){
reloadWindow(window);
});
var ga = new GlideAjax('CatalogUtils');
ga.addParam('sysparm_name', 'checkCartLimit');
ga.getXMLWait();
var answer = ga.getAnswer();
if (answer == 'exceeded') {
g_form.addErrorMessage('You can only add up to 3 items to your cart. Clicking the Order Now button will order this item only. Clicking the Proceed to Checkout button will order all of the items in the Shopping Cart.');
document.getElementById('oi_add_to_cart_button').style.display = "none"; //to hide Add to Cart button
}
}
Here's the Script Include:
var CatalogUtils = Class.create();
CatalogUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkCartLimit: function(){
var answer = 'false';
var id = gs.getUserName();
var count = 0;
var cartItems = new GlideRecord('sc_cart_item');
cartItems.addQuery('cart.sys_created_by', id);
cartItems.orderByDesc('sys_created_on');
cartItems.query();
while (cartItems.next()) {
count++;
if (count >= 3) {
answer = 'exceeded';
break;
}
}
return answer;
},
type: 'CatalogUtils'
});
The first part of the Client Script reloads the form when Add to Cart is clicked so that the cart quantity check re-runs, so take this first block out if you're already doing something like that - out of box the request form stays loaded with the variables populated when Add to Cart is clicked. I've noticed that when doing this the last item added to the cart isn't displayed in this list. The item is actually in the cart and will be shown at checkout or when editing the cart. With a 20 item limit, this hasn't been a huge issue for us, so I never pursued it. This post points to a UI Macro addition that should do the reload, but I couldn't get it to work when I was putting this solution together
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 06:43 AM
Hi Brad,
We have around 954 catalog items, so it's impossible to add the variable set to all of these.
Business rule is working fine as expected but only problem is that error is popping up even when i check out with 3 items in cart.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 08:09 AM
The only way to clear an error message without the user clicking the X icon is with g_form.clearMessages() in an onSubmit or onLoad client script. You should be able to script adding a variable set to all Catalog Items if that approach is needed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2025 12:50 PM
In the approach you mentioned is it looking at number of times each item is added to cart ?
if ABC catalog item is added to cart 3 times then throw that error msg ?
in my case it could be any request added to cart , we should limit number of items to 20 which is the actual requirement.
So user can add same items multiple times or different items but max a user can add to cart is 20. This is what i am trying to achieve.