2

I have a Shopify ruby code, that basically triggers a price discount at checkout when a valid code is used. 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.

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.

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?

  • Can I convert the values first to strings and then use the contain operator?

bad_coder
  • 11,289
  • 20
  • 44
  • 72

1 Answers1

2

Ruby does not have a contains operator, but it has the include? method which will solve your problem:

if Input.cart.discount_code and Input.cart.discount_code.code.include? "TEST"
  ...
end
davidthorand
  • 393
  • 2
  • 7