简体   繁体   English

Shopify 限制产品订购数量

[英]Shopify limit product order quantity

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.我是 Shopify 的新手,想知道如何将产品的最大订购数量设置为特定数量。

So product A max order qty 6所以产品 A 最大订单数量 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?代码更改还会在 Shopify 更新后继续存在吗? Sorry I am switching over from Wordpress so getting familiar with this new system.抱歉,我要从 Wordpress 切换过来,所以要熟悉这个新系统。

Many Thanks!非常感谢!

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.在 HTML 输入元素上设置最大属性或尝试使用 javascript 限制产品。 Note this is not recommended as customers can find an easy workaround for that limit using /cart/add/{:variantid} permalink.请注意,不建议这样做,因为客户可以使用 /cart/add/{:variantid} 永久链接找到针对该限制的简单解决方法。
  2. Use an app to limit quantity from the Shopify app store.使用应用程序从 Shopify 应用程序商店限制数量。
  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.甚至对 /cart.js 的 API 调用也会得到计算数量。 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请注意,脚本编辑器适用于 Shopify Plus 用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM