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

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

1 Like

Thanks, I will try these steps

1 Like

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

3 Likes

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!

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:

  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.

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:

  1. Open the app and click on the “Add a new rule” button.
  2. Click “Add a new condition” and select the “Single item quantity” option.
  3. Add 3 in the “Block if more than” field.
  4. In the “Error message” field, add an appropriate message which will be displayed in the checkout page.
  5. Save the rule.

That’s it. Now all customers must order under 3 quantity per item.

This was so helpful. Thank you!

I replicated the same for the Cart page in Sections code file called main-cart-items.liquid


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.

Hi @christine_27_27,

I saw your question about cart quantity limits. In DC Order Limits, you can restrict how many units a customer can add for a product.

Setup:

  1. Create an order limit rule with a max quantity of 3

  2. Apply it to all products, or only specific products or collections

  3. If they try to add more than 3, they will see an error and checkout is blocked

Cart quantity limits are available on the basic plan. Tracking limits across past orders is a Pro feature.

Let me know if this setup works for you, happy to help if you run into anything.