Shopify limit product order quantity

2.9k Views Asked by At

I am new to Shopify and would like to know how I can set the max order quantity of a product to a specific number.

So product A max order qty 6

I am using the Symmetry theme.

I am comfortable editing code, just need to know where to find it. Also will a code change survive Shopify updates? Sorry I am switching over from Wordpress so getting familiar with this new system.

Many Thanks!

1

There are 1 best solutions below

4
On

There are at least three ways you can achieve that:

  1. Set max attribute on HTML input element or try to limit product using javascript. Note this is not recommended as customers can find an easy workaround for that limit using /cart/add/{:variantid} permalink.
  2. Use an app to limit quantity from the Shopify app store.
  3. The best approach would be to use Script Editor because customers cannot find a way to bypass that limitation. It works on both: the cart and checkout page. Even API calls to /cart.js will get computed quantity. I wrote a small snippet that works well:
class LimitProduct
  def initialize(variantId, quantity)
    @variant = variantId
    @max_qty = quantity
  end

  def run(cart)
    line_items = cart.line_items.select { |line_item| line_item.variant.id == @variant }
    return if line_items.empty?

    loop_items(cart, line_items)
  end

  def loop_items(cart, line_items)
    if @max_qty==0
      cart.line_items.delete_if {|item| item.variant.id == @variant }
    else
      line_items.each_with_index do |item,index|
        if item.quantity > @max_qty && @max_qty != 0
          reduceBy = item.quantity - @max_qty
          item.split(take: reduceBy)
        end
      end
    end
  end
end

CAMPAIGNS = [
  LimitProduct.new(1234567890, 0), #first arg is variant id, second is max quantity
  LimitProduct.new(1111111111, 2)
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart

Note that Script Editor is for Shopify Plus users