Limit items in cart to 20 items only

Vijay147
Tera Contributor

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.

 

 

Vijay147_0-1741112046333.png

 

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.

Vijay147_1-1741112502490.png

 

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.

0 REPLIES 0