Limiting the Total count of items in the cart

@LitExtension

@KetanKumar

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.

www.aadesgins.store

Sample screenshots:

Any support in terms of solution from anyone amongst the community will be appreciated.

If any one knows solution to this problem or any alternate solution. Please share and help.

Any helpful response from any community member?

@LitExtension

@KetanKumar

Any response on my query? Can you please support?

Or can anyone else from shopify community help? What’s its purpose if no one’s even responding?

Hello @ahsan_dgins

I don’t know how to help you with the code honestly for this requirement.

but just want to let you know that our app has this feature already if you want to try

https://apps.shopify.com/purchase-limit?