Want personalized guidance adding this to your store?
Check out our Insiders community: https://www.skool.com/the-prompted
Members of The Prompted community receive a detailed store audit, 1-on-1 guidance for implementing new features, and access to an exclusive theme. You'll also get marketing support, the same tactics we use to spend over $100k/mo on Meta Ads.
---
In this customization, we are adding product order limits, so you can set the maximums and minimums that customers can buy.
You might not want your customers to over-order a promotion so you can set a maximum. Or if you need to save on shipping costs you can set a minimum so they purchase in multiples.
Normally this is a feature that you have to pay for by installing an app, but in this video I’m going to show how to add it to your store for free.
Compatible Themes: This code should work on all free Shopify themes (Dawn, Refresh, Craft, Studio, Publisher, Crave, Origin, Taste, Colorblock, Sense, Ride, Spotlight).
Create metaobject and metafields
- Create quantity_rule metaobject with fields name, increment, min, and max
- required
- integer, min value 1
- Create Product metafield quantity_rule with metaobject type quantity_rule
- Create Variant metafield quantity_rule with metaobject type quantity_rule
- Create metaobject entries
Edit Theme Code
Edit settings_schema.json
{
"name": "Custom Quantity Rules",
"settings": [
{
"type": "checkbox",
"id": "use_quantity_rules",
"label": "Enable custom quantity rules",
"default": false
},
{
"type": "checkbox",
"id": "use_total_product_quantity",
"label": "Use total product quantity",
"info": "Add the total of all variant quantities together",
"default": false
}
]
}
Edit main-cart-items.liquid and cart-drawer.liquid
- Replace
item.variant.quantity_rule
withquantity_rule
- Add the following code:
{%- if settings.use_quantity_rules -%} {%- if settings.use_total_product_quantity -%} {% assign quantity_rule = item.product.metafields.custom.quantity_rule.value %} {% assign total_product_quantity = 0 %} {% assign global_max = item.product.metafields.custom.quantity_rule.value.max | plus: 0 %} {% for cart_item in cart.items %} {% if cart_item.product.id == item.product.id %} {% assign total_product_quantity = total_product_quantity | plus: cart_item.quantity %} {% endif %} {% endfor %} {% assign item_remaining_qty = global_max | minus: total_product_quantity %} {% assign max_for_variant = item.quantity | plus: item_remaining_qty %} {% if max_for_variant > global_max %} {% assign max_for_variant = global_max %} {% endif %} {% else %} {% assign quantity_rule = item.product.selected_or_first_available_variant.metafields.custom.quantity_rule.value %} {% endif %} {% else %} {% assign quantity_rule = item.variant.quantity_rule %} {% endif %}
- Replace the input max
max="{{ quantity_rule.max }}"
with:{% if settings.use_total_product_quantity %} max="{{ max_for_variant }}" {% else %} max="{{ quantity_rule.max }}" {% endif %}
Edit main-product.liquid
- Replace
product.selected_or_first_available_variant.quantity_rule
withquantity_rule
- Add the following code:
{% if settings.use_quantity_rules %} {% if settings.use_total_product_quantity %} {% assign quantity_rule = product.metafields.custom.quantity_rule.value %} {% else %} {% assign quantity_rule = product.selected_or_first_available_variant.metafields.custom.quantity_rule.value %} {% endif %} {% else %} {% assign quantity_rule = product.selected_or_first_available_variant.quantity_rule %} {% endif %} {% if settings.use_quantity_rules and settings.use_total_product_quantity %} {% assign cart_qty = 0 %} {% for item in cart.items %} {% if item.product.id == product.id %} {% assign cart_qty = cart_qty | plus: item.quantity %} {% endif %} {% endfor %} {% else %} ... existing cart_qty assignment... {% endif %}