某些产品 shopify 脚本的块折扣

Block discount on certain product shopify script

我想在 Shopify 中编写一个脚本,以防止在某些产品上呈现折扣。我知道这是错误的,但像这样:

Input.cart.line_items.each do |line_item|
  product = line_item.variant.product

  if product = 123456789
     CartDiscount.remove("Discount does not apply")
  end

end

Output.cart = Input.cart

我查看了 documentation 并看到了 .reject({ message: String }) 方法,但它适用于整个购物车。有没有办法将其本地化到一个实例?

打折很容易。首先...从购物车中获取折扣代码。你知道它适用于整个购物车。现在您有一个金额、百分比或您在代码中给客户的任何内容。现在开始介绍脚本的美妙之处。

对于每个产品,决定它是否打折。如果是,请应用您从提供给该项目的折扣代码中获知的折扣金额。如果是不打折的商品,价格就别管了。

您为脚本引擎付出了高昂的代价。如果您不能自己编程,请付钱给可以编程的人!你最好研究一个工作脚本来学习,而不是试图战胜自己。

因此您实际上可以拒绝购物车的折扣代码,但仍然可以对一些、不是全部或其他产品进行折扣。

老问题,但我找到了解决方法:基本上,您不能 "localize" 对特定产品打折。您必须阻止整个购物车获得折扣。我最终采用的方法如下:

# ID of product you want to block
productId = 10199241991

# Runs through a loop of items in your cart
Input.cart.line_items.each do |line_item|
  product = line_item.variant.product
  puts product.id
  next if product.gift_card?
  next unless product.id == productId
  case Input.cart.discount_code
  when CartDiscount::Percentage
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  end
end


Output.cart = Input.cart