1

I currently have a working Shopify/Ruby script that blocks discount codes if a certain product is in the cart. It looks like this:

productid = 1234567890

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"})
  when CartDiscount::FixedAmount
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  when CartDiscount::Shipping
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  end
end

Output.cart = Input.cart

I want to change it now to still deny using discount codes on this given product, but allow it if a very specific discount code is used. I tried the following 2 approaches:

1:

productid = 1234567890

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 == ma770id
  case Input.cart.discount_code
  when cart.discount_code != 'yesdiscount'
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  end
end


Output.cart = Input.cart

2:

productid = 1234567890

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 != 'yesdiscount'
  when CartDiscount::Percentage
    Input.cart.discount_code.reject({message: "Cannot be used with This Product"})
  when CartDiscount::FixedAmount
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  when CartDiscount::Shipping
    Input.cart.discount_code.reject({message: "Cannot be used with this product"})
  end
end

Output.cart = Input.cart

When I insert a code that is not yesdiscount the discount still applies even though I have a check to see if the code is not equal to the approved code.

My first thought was syntax, but have confirmed that the not equal operator is in fact !=. What is logically happening here that is causing discounts to accept, even though I'm checking for it to not be the one I want to work?

kawnah
  • 3,204
  • 8
  • 53
  • 103

1 Answers1

1

Your case syntax is incorrect, case is used like below:

# a is variable, if a contains val1 it goes to first when, if val2 it goes to second when, if neithr it goes to else
case a
when 'val1'
when 'val2'
else
end

Other ways of using case is listed here

Applying correct case syntax to first approach:

productid = 1234567890

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 == ma770id
 case Input.cart.discount_code
 when 'yesdiscount'
      #code here if yesdiscount is discount_code
 else 
  Input.cart.discount_code.reject({message: "Cannot be used with this product"})
 end
end
ugandharc18
  • 651
  • 5
  • 7