How to limit the cart to MAX CART ITEMS

Setting the cart items limit is becoming a frequent request and there is no such behavior/setting out-of-the-box.
Implement this by custom code in Storefront: change current https://github.com/VirtoCommerce/vc-storefront-core/blob/master/VirtoCommerce.Storefront/Domain/Cart/CartBuilder.cs#L609-L613 code

    else
    {
        lineItem.Id = null;
        Cart.Items.Add(lineItem);
    }

to this

    else if (Cart.Items.Count < cartSizeLimit)
    {
        lineItem.Id = null;
        Cart.Items.Add(lineItem);
    }

Note, that the cartSizeLimit could be just a constant in your code or a store-specific setting value, defined by the store manager. It’s up to you to define and initialize the value correctly.

P.S. It makes sense to repeat this logic in your theme code as well.

1 Like