Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
Hi Everyone,
I am trying to set a minimum quantity to a specific product that we offer. All other products are still fine at 1, but a particular product type needs to be a minimum of 4. How would I change this to start the order at 4 but allow the customer to change the quantity to anything equal to or higher?
I've looked through other similar requests and have looked around the product-template.liquid code but I don't think that's what I want since it seems to change the minimum of everything instead of a single product.
Further note, this product changes every few months for a new promotion and art, so it would be exceptionally useful if I could somehow tie this minimum restriction to a given tag. So any time I add the tag MIN4, it applies the minimum 4 rule.
Is that possible? Thanks!
@bcomst this is possible with a little custom coding.
Here's how I would approach it:
1) Add the MIN4 tag to the products you want minimum
2) In the product form file (product-form.liquid, or product.liquid, etc whatever has the quantity / add to cart button), create a script that runs if that tag is present, something like this:
<p id="error-msg" style="display: none; color: red;">This product requires a quantity of 4 minimum.</p>
{% if product.tags contains "MIN4" %}
<script>
// Example to set a minimum on the input box
let qty = document.querySelector("#quantity");
qty.min = 4;
// I would add a backup, for example if you're using a select box or if the user's browser ignores the minimum attribute
document.addEventListener("click", (event) => {
let qtySelected = document.querySelector.value;
if (event.target.innerText.indexOf("Add to Cart") >= 0 && qtySelected < 4) {
// Reveal an error message
document.querySelector("#error-msg").display = block;
}
});
</script>
{% endif %}
3) If you're showing an error message, you could also add some code to clear the message when quantity of 4 is selected (set an event listener on the quantity box).
The exact code will depend on your product page setup, but that code above will help you get started, experiment with that and swap out what you need.
If you prefer to have a professional developer handle it for you, feel free to reach out to my team at speedboostr.com/contact - our developers work on Shopify sites every day to do customizations like this.
I've tried your code and found that if the product contains several options, the min limitation will fail when the option change.
So I made some update of the code, which set the default quantity of the variant selection to the minimum requirement, and the limitation can be applied to all the variant.
It should be pasted to the main-product.liquid, and find the quantity section:
<span class="input-amount">
<label for="quantity" class="hidden">{{ 'product.form.quantity' | t }}</label>
<input type="number" id="quantity" name="quantity"
{% if product.tags contains "MIN4" and current_variant.inventory_management == 'shopify' -%}
value="4" min="4"
{% else %}}
value="1"
{% endif %}
{% if current_variant.inventory_management == 'shopify' and current_variant.inventory_policy == 'deny' -%}
max="{{ current_variant.inventory_quantity }}"
{% endif %} required>
</span>
Hello, good afternoon
this code is exactly what I need
but I don't know where to put it
can you help me please?
I don't know if I have to delete anything from the original code.
Ou est ce qu'il faut mettre exactement le morceau de code dans le main-product.liquid?
Thanks for the code @KikyChen but for anyone finding this you also need to edit the main.cart file and any cart drawer liquid file with similar code otherwise they can just reduce the quantity there after adding product to cart. Also, warning "tags" don't appear to be a valid selector in the cart or cart drawer, maybe use slug or title as qualifier.
Thanks for this code it works well for me where I have a MOQ, customers can increase the quantity using the arrow which is great, they cannot reduce the quantity using the arrow however they can manually type over the quantity which means they can order less than the minimum. Any way to stop this? Also @epidote mentioned this code needs to be added to the cart.liquid file, where in the liquid cart file should the code be added?
Hi CymCards, actually this code is not a 100% foolproof solution. There are multiple ways you can bypass the code. In 2024, I would recommend using an app like Cart Lock which can force to meet a minimum quantity for some specific products like using product tags and this restriction will work on the checkout page as well.
That's it. Now the customers must purchase 4 or more quantity for the MIN4 tagged products.
Hi @bcomst
You could consider using third-party apps such as Order Limits ‑ MinMaxify. They help you define minimum and maximum product and cart limits for your orders, from simple to complex.
I hope this helps you.
Hi, do you know if it is possible to exclude MOQ from certain customers? I am looking or something similar on our store. We sell salon supplies but also have our own salons, which they order their stock off our store at special wholesale prices through another app as a tagged customer.
For a particular product, we want general customers to purchase MOQ of 5, but for our salons, they have no minimum order requirements. Is this possible with this app?
Hey, @bcomst
I'd also suggest taking a look at the app OrderLogic by Oiizes. With this app, you can set limits on how few or how many of each product your customers can order. If there is anything else I can help you with, please let me know.
If there is anything else I can help you with, please let me know.
Dirk | Social Care @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Hi Dirk, do you know if it is possible to exclude MOQ from certain customers? I am looking or something similar on our store. We sell salon supplies but also have our own salons, which they order their stock off our store at special wholesale prices through another app as a tagged customer.
For a particular product, we want general customers to purchase MOQ of 5, but for our salons, they have no minimum order requirements. Is this possible with this app?
Hello, Have you got the solution? If not, then you can try the MultiVariants - Bulk order app.
You need to create a product in Shopify and under the main product create variants that will be visible on the product page. So, all the variants will display under the main product and there will be a quantity selector for each variant. Your customer can choose their own combination of all products.
With this app, you can set the min and max quantity, so your customer must choose that quantity before going to the cart page. Additionally, through this app, you can apply restrictions for minimum-maximum quantities, predefined bundles, interval quantities, limits on variants, and orders that can be achieved. These restrictions can be set specifically on any particular product or a specific group of products also by using type, tag etc. Restrictions will be working also on non-variant products also.
You’ll get 3 days of trial along with great customer support for exploring all other features.
Here is the Sample product and here is the Sample store with more available features.
Restriction for Non-variant
You probably already found a solution but in case someone else visit this post, on the main-product.liquid file, probably under sections, look for the input type "quantity" html tag, should look like this:
<input
name="quantity"
type="number"
class="quantity-input-field"
id="quantity-input-{{ section.id }}"
min="1"
value="1"
form="{{ product_form_id }}"
>
copy the piece of code and change the property "min" to whatever is your MQO, that new code should be inside a conditional that will check a product by tag, in my case is the tag MQO10, below you will find the before and after code was updated:
BEFORE:
AFTER:
BEFORE:
<input
name="quantity"
type="number"
class="quantity-input-field"
id="quantity-input-{{ section.id }}"
min="1"
value="1"
form="{{ product_form_id }}"
>
AFTER:
<!-- MQO FOR STAKES - BY JENNIFER -->
<!-- Conditional added (if statement) to acomplish this -->
{% if product.tags contains 'MQO10' %}
<!-- Conditional added (if statement) to acomplish MQO -->
<input
name="quantity"
type="number"
class="quantity-input-field"
id="quantity-input-{{ section.id }}"
min="10"
value="10"
form="{{ product_form_id }}"
>
<!-- END OF MQO FOR STAKES - BY JENNIFER -->
{% else %}
<!-- Conditional added (if statement) to acomplish MQO -->
<input
name="quantity"
type="number"
class="quantity-input-field"
id="quantity-input-{{ section.id }}"
min="1"
value="1"
form="{{ product_form_id }}"
>
{% endif %}
<!-- Conditional added (if statement) to acomplish MQO -->