首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shopify脚本:拒绝折扣不被考虑

问Shopify脚本:拒绝折扣不被考虑
EN

Stack Overflow用户
提问于 2022-11-22 01:17:44
回答 1查看 18关注 0票数 0

我需要拒绝折扣,但我也希望只适用于某些产品的折扣金额。

例如:我的购物车是A,B,我有10%的订单折扣。我不想B产品打折,但我只想打折A,我的方法是这样

代码语言:javascript
复制
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,因为不再有折扣。

我试图在属性中保存折扣值,但是在下一次运行时会丢弃它们。无法将信息从脚本的一次运行传递到另一次。

有可能知道被拒绝的折扣的折扣率吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-23 00:36:12

我认为你根本不需要拒绝折扣代码。您可以简单地添加一个条件,在应用折扣时控制该条件,例如:

代码语言:javascript
复制
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条件下使用它。无论哪种方式,拒绝的方向都是错误的,就我个人而言,我只使用过一次,以防止顾客滥用非常复杂的活动(这是一个例外)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74526502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档