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?