Hi,
I have seen that you were able to help others and I am wondering if you can please help me?
I have multiple products in my store with different variants. I want to limit the total no. of items that can be added to cart (or that are present in cart). Let’s say, I want to limit it to 5.
For this, I have added the following code in main-cart-items.liquid and main.product.liquid.
I have Dawn Theme 10.0 (I am not a professional developer. I am just trying to optimize settings for the store).
main-cart-items.liquid
Under the section
and under the line
min=“{{ product.selected_or_first_available_variant.quantity_rule.min }}”
I have added b/m code:
{% assign maxcartcount = 5 %}> > {% if cart.item_count <= maxcartcount %}> > max=“{{ maxcartcount |minus: cart.item_count |plus: item.quantity }}”> > {% endif %}
main.product.liquid
Under the section
and under the line
data-min=“{{ product.selected_or_first_available_variant.quantity_rule.min }}”
min=“{{ product.selected_or_first_available_variant.quantity_rule.min }}”
I have added b/m code:
{% assign maxcartcount = 5 %}> > {% if cart.item_count <= maxcartcount %}> data-max=“{{ maxcartcount |minus: cart.item_count |plus: item.quantity }}”> max=“{{ maxcartcount |minus: cart.item_count |plus: item.quantity }}”> {% endif %}
This is working fine except that negative numbers appear in quantity selector on product page when I select 2-3 different products/variations and add them to cart.
For example, if I select product-A with 3 quantites and add it to cart. Then I won’t be able to press PLUS to add up 2 more quantities to this variant and quantity selector would show - 1 at first. (Both plus and minus are grayed out for this product / variant).
All other products are available to be added to cart in normal way. Selecting upto 2 quantites as 3 out of 5 are already added in cart.
Besides, when I add another variation/product-B with 2 quantities, then the quantity selector would show - 2 on this variant (Product B) and -3 on previous variant (Product A).
If I am on any other product’s page, I won’t be able to add it to cart as maximum 5 items are already added to cart; But, If I am on any of these 2 variants/products (Product A or Product B) with -ve number on quantity selector and click on Add to Cart, this will be added to cart making it 6 items in the cart and so on if I keep clicking on Add to Cart.
I don’t understand why it is showing negative numbers. I want to figure it out and get rid of this error. Please support me in this.
Sample screenshots:

