简体   繁体   English

某些产品shopify脚本的块折扣

[英]Block discount on certain product shopify script

I want to write a script in Shopify that prevents discounts from rendering on certain products. 我想在Shopify中编写一个脚本,以防止某些产品出现折扣。 I know this is wrong, but something like this: 我知道这是错误的,但类似这样:

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

I looked at the documentation and saw the .reject({ message: String }) method but it applies to the whole cart. 我查看了文档 ,看到了.reject({ message: String })方法,但该方法适用于整个购物车。 Is there a way to localize this to one instance? 有没有一种方法可以将其本地化到一个实例?

Old question but I found a resolution: basically, you can't "localize" a discount to a specific product. 老问题了,但我找到了解决方法:基本上,您不能将折扣“本地化”到特定产品。 You have to block the entire cart from getting a discount. 您必须阻止整个购物车获得折扣。 The approach I ended up taking is below: 我最终采用的方法如下:

# 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

Discounts are easy. 打折很容易。 First... get the discount code from the cart. 首先...从购物车中获取折扣码。 You know it applies to the whole cart. 您知道它适用于整个购物车。 Now you have an amount, or percentage or whatever you gave the customer in the code. 现在您有了金额,百分比或您在代码中给客户的任何内容。 Now kicks in the beauty of the scripts. 现在可以欣赏脚本的优美之处。

For each product, decide if it is discounted or not. 对于每种产品,决定是否打折。 If it is, apply the discount amount that you know from the discount code provided to the item. 如果是,则应用从提供给商品的折扣代码中知道的折扣金额。 If it is a product not to be discounted, leave the price alone. 如果是不打折的产品,请不要理会价格。

You are paying big bucks for the scripting engine. 您为脚本引擎付出了高昂的代价。 If you cannot program it yourself, pay someone that can! 如果您自己不能编程,请付钱给可以的人! You are way better off studying a working script to learn, that trying to outfox yourself. 您最好学习一个工作脚本来学习,以使自己超越自我。

So you can in fact reject the discount code for the cart, but still discount products, some, not all, or others. 因此,您实际上可以拒绝购物车的折扣代码,但仍可以折扣某些或部分而非全部的产品。

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

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