Shopify Scripts: Rejected discount is not considered
I don't think you need to reject the discount code at all. You can simply add a condition that controls when the discount is applied, for example:
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
This way you are avoiding hackish solutions. Instead of checking for skus you can check for tags, title, and so on. The full list can be found here: https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#variant (https://help.shopify.com/en/manual/checkout-settings/script-editor/shopify-scripts#variant)
You can also create a null variable, run some for loops in advance and set true/false flag to that variable and use it in your if condition. Either way, the direction of rejection is the wrong way and personally, I have used it only once to prevent customers from abusing a very complex campaign (which was an exception).
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.