我需要拒绝折扣,但我也希望只适用于某些产品的折扣金额。
例如:我的购物车是A,B,我有10%的订单折扣。我不想B产品打折,但我只想打折A,我的方法是这样
if cart.discount_code&.percentage
discount = cart.discount_code.percentage / 100.0
discount = 1.0 - discount.to_s.to_f # there's probably a better way to do this... but :)
cart.line_items.each do |line_item|
line_item.change_line_price(line_item.line_price * discount, message: "Applying discount code: " + cart.discount_code.code)
end
cart.discount_code.reject(message: "The discount cannot be applied.")
end请注意,这在输入测试中有效。
问题是,(根据我的经验发现) Shopify一旦被拒绝,就会取消折扣。尽管API提供了一个rejected?方法。而且,由于删除折扣再次触发脚本,第二次跳过if,因为不再有折扣。
我试图在属性中保存折扣值,但是在下一次运行时会丢弃它们。无法将信息从脚本的一次运行传递到另一次。
有可能知道被拒绝的折扣的折扣率吗?
发布于 2022-11-23 00:36:12
我认为你根本不需要拒绝折扣代码。您可以简单地添加一个条件,在应用折扣时控制该条件,例如:
if Input.cart.discount_code&.percentage
discount = Input.cart.discount_code.percentage / 100.0
discount = 1.0 - discount.to_s.to_f # there's probably a better way to do this... but :)
Input.cart.line_items.each do |line_item|
if line_item.variant.skus.include? '3033358' # check if variant SKU is 3033358
line_item.change_line_price(line_item.line_price * discount, message: "Applying discount code: " + Input.cart.discount_code.code)
end
end
end
Output.cart = Input.cart这样你就可以避免恶意的解决方案了。您可以检查标签、标题等,而不是检查skus。完整的列表可以在这里找到:https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#variant
您还可以创建一个空变量,预先运行一些for循环,并将true/false标志设置为该变量,并在您的if条件下使用它。无论哪种方式,拒绝的方向都是错误的,就我个人而言,我只使用过一次,以防止顾客滥用非常复杂的活动(这是一个例外)。
https://stackoverflow.com/questions/74526502
复制相似问题