How to set a max quantity limit per item in cart?

How to set a max quantity limit per item in cart?

christine_27_27
Visitor
2 0 1

I'm looking for guidance on how to set a maximum quantity limit per item in the cart for my store. Specifically, I want to restrict customers from adding more than 3 quantities of a single item to their cart. If, for instance, a customer already has 3 quantities of an item in the cart and they try to add more, I'd like to have an alert to inform them that the maximum quantity has been reached for that particular item. 

I appreciate any insights or code snippets that can help me achieve this functionality on my Shopify store. Thank you in advance for your assistance!

Replies 6 (6)

Guleria
Shopify Partner
3393 675 955

Hello @christine_27_27 ,

 

You need to apply the  logic on PDP page.
First step, you need to check the quantity selection on click of the atc button. If it's greater than 3 you have to restrict the default functionality of the atc button and show a message that you can't add more than 3.  
Second step, if customer quantity selection is just 1 and they use same process to add more than 3 items then in this case you have read the cart on every click of the atc button through ajax and If same product counts is greater than 3 show the message and restrict the customer to add product to cart.

Third step, in the cart page make a check on the quantity selector so the customer cannot increase quantity more then 3.  

btw just want to let you know it's not possible to provide code snippets for it because it needs changes in multiple files and a/to the theme you are using.

 

Thanks

- If helpful then please Like and Accept Solution.
- Drop an email   if you are looking for quick fix or any customization ( paid services )
- Email: guleriathakur43@gmail.com
- Try GEMPAGES a great page builder
christine_27_27
Visitor
2 0 1

Thanks, I will try these steps

bulogyx
Tourist
3 0 1

I didn't understand the solution provided by Developer-G.

 

I was able to achieve this simply in the Dawn theme... setting a max limit that applies to all products. Other themes should have similar code that can be modified. For the Dawn theme, go to the theme and select the ellipsis button and "edit code." Go the the Sections code file called main-product.liquid. There you will find the following area:

 

<input
class="quantity__input"
type="number"
name="quantity"
id="Quantity-{{ section.id }}"
data-cart-quantity="{{ cart_qty }}"
data-min="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
min="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
{% if product.selected_or_first_available_variant.quantity_rule.max != null %}
data-max="{{ product.selected_or_first_available_variant.quantity_rule.max }}"
max="{{ product.selected_or_first_available_variant.quantity_rule.max }}"
{% endif %}
step="{{ product.selected_or_first_available_variant.quantity_rule.increment }}"
value="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
form="{{ product_form_id }}"
>

What you want to do is set those data-max and max values. It's interesting that the code exists for there to be max (and min) limits, but there is no way to set them from the shopify product or store settings. I changed my code as follows to comment out the unused lines and hard code the max values:

 

<input
class="quantity__input"
type="number"
name="quantity"
id="Quantity-{{ section.id }}"
data-cart-quantity="{{ cart_qty }}"
data-min="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
min="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
{% comment %}{% if product.selected_or_first_available_variant.quantity_rule.max != null %}
data-max="{{ product.selected_or_first_available_variant.quantity_rule.max }}"
max="{{ product.selected_or_first_available_variant.quantity_rule.max }}"
{% endif %} {% endcomment %}
data-max="5"
max="5"
step="{{ product.selected_or_first_available_variant.quantity_rule.increment }}"
value="{{ product.selected_or_first_available_variant.quantity_rule.min }}"
form="{{ product_form_id }}"
>

 

I hope that helps. If you need different limits for different sets of products, that could be done relatively easily using tags or collections. Note that min can also be set, again applying to all products here unless conditions are set with if statements.

 

-Barb

BinkBox
Visitor
1 0 0

Hey this code worked perfectly for me. Thank you.

I was hoping you could write some code for me to do the same thing but on the cart page. The code you wrote works for products, but once they add the products to their cart they are able to adjust the quantity on the cart page. I want to make it so that they are not able to adjust the quantity on the cart page.

Thanks! 

colleenc
Shopify Partner
91 9 21

I have been looking for the same solution and discovered that the code for limiting quantities is already in our theme code, but the input fields for limits are not available in our product admin. It is standard on higher levels of Shopify stores. Anyhow, I bumped into a tutorial on the Shopify site that includes all the code necessary and the details of how and where to add this code. I have not added it to my store yet to test it. I suggest strongly that you back up your store before adding the code, and also that you try it on a development site first. The tutorial is located here: https://help.shopify.com/en/manual/b2b/theme-code/quantity-pricing#quantity-rules

Note: I haven’t implemented this yet, but will over the next few days.

basilkhan
Shopify Partner
49 0 5

Hi @christine_27_27,

You can set a maximum quantity limit per item in the cart without needing to code by using the DC Order Limits app:

  1. Create a New Rule: Go to the DC Order Limits section in your Shopify admin dashboard and create a new order limit rule.
  2. Set Maximum Quantity: In the rule details, set the "Maximum Quantity" to 3. This ensures customers can’t add more than 3 quantities of a single item to their cart.
  3. Configure Grouping Options: Select "Product Variant" so the limit applies to each variant individually.

basilkhan_0-1718500474560.png

 

This way, if a customer tries to add more than 3 quantities of an item, they'll be notified that the maximum quantity has been reached for that item. Take a look here for a video demo if you are interested: https://help.dashcheckout.io/article/57-setting-up-order-limits-cart-limits-in-shopify

 

Co-founder and Product Developer at Freshly Commerce ️ | Building Simple Bundles, Simple Discounts, and Freshly Inventory