简体   繁体   English

在 Shopify Ruby 中为 cart.discount_code 使用“包含”运算符 - 不工作

[英]Using "contain" operator in Shopify Ruby for cart.discount_code - Not working

I have a Shopify ruby code, that basically triggers a price discount at checkout when a valid code is used.我有一个 Shopify ruby​​ 代码,当使用有效代码时,它基本上会在结账时触发价格折扣。 I use a script instead of a normal Shopify discount because I need to manually limit the total amount discounted when reached a max basket total.我使用脚本而不是普通的 Shopify 折扣,因为我需要在达到最大购物篮总数时手动限制折扣总额。

The problem comes when I need to match the discount script to my voucher(s) code , because I'm using 10000 bulk codes with the same prefix (set to 0% discount) and I cant use the operator contain , and it will only work with == , which is an exact match.当我需要将折扣脚本与我的优惠券代码匹配时,问题就出现了,因为我使用了 10000 个具有相同前缀的批量代码(设置为 0% 折扣)并且我不能使用运算符contains ,它只会使用== ,这是完全匹配的。

This limits to only one voucher code my script can understand and I would like for the system to only take into account the prefix of the code introduced at the basket so all bulk discounts can trigger the rest of the ruby code这仅限于我的脚本可以理解的一个优惠券代码,我希望系统只考虑篮子中引入的代码的前缀,因此所有批量折扣都可以触发其余的红宝石代码

Example working:示例工作:

if Input.cart.discount_code and Input.cart.discount_code.code == "TEST"

Example not working:示例不起作用:

if Input.cart.discount_code and Input.cart.discount_code.code contains "TEST"

The first code works perfectly, but the second one triggers a production error.第一个代码完美运行,但第二个代码触发了生产错误。

  • Is it because the Input.cart.discount_code and Input.cart.discount_code.code are not string values and therefore is not understood by the operator?是不是因为Input.cart.discount_code 和 Input.cart.discount_code.code不是字符串值,因此操作员不理解?

  • Can I convert the values first to strings and then use the contain operator?我可以先将值转换为字符串,然后使用包含运算符吗?

Thanks!谢谢!

Ruby does not have a contains operator, but it has theinclude? Ruby 没有contains运算符,但它有include? method which will solve your problem:可以解决您的问题的方法:

if Input.cart.discount_code and Input.cart.discount_code.code.include? "TEST"
  ...
end

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

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