Shopify themes, liquid, logos, and UX
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!
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
Thanks, I will try these steps
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
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!
This was so helpful. Thank you!
I replicated the same for the Cart page in Sections code file called main-cart-items.liquid
<input
class="quantity__input"
data-quantity-variant-id="{{ item.variant.id }}"
type="number"
name="updates[]"
value="{{ item.quantity }}"
{% # theme-check-disable %}
data-cart-quantity="{{ cart | item_count_for_variant: item.variant.id }}"
min="0"
data-min="{{ item.variant.quantity_rule.min }}"
{% comment %}{% if item.variant.quantity_rule.max != null %}
max="{{ item.variant.quantity_rule.max }}"
{% endif %}{% endcomment %}
max="5"
step="{{ item.variant.quantity_rule.increment }}"
{% # theme-check-enable %}
aria-label="{{ 'products.product.quantity.input_label' | t: product: item.product.title | escape }}"
id="Quantity-{{ item.index | plus: 1 }}"
data-index="{{ item.index | plus: 1 }}"
>
For future visitors to this page looking for answers: the reason the fields for `quantity_rule` are not available in most shop admins is because they are only for B2B catalogs, which are only available for Plus stores.
Regardless, be aware that quantity limits can't truly be limited from the frontend (website) only, anyone who knows how to open the developer tools and type in a fetch call to /cart/add.js will be able to add as many items as they want. The only way to truly control it is with an app.
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.
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:
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
It is possible to set maximum quantity of a single item easily by using the Cart Lock app easily. Follow the below steps:
That's it. Now all customers must order under 3 quantity per item.
Shopify and our financial partners regularly review and update verification requiremen...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025