Discount Script applies discounts even if underlying code is rejected

Topic summary

Shopify Scripts discount condition appears to run even when the related discount code is rejected at checkout. The author checks the code with Input.cart.discount_code&.code == "CYBER2023" && !Input.cart.discount_code&.rejected? (Ruby safe-navigation), expecting the script to only run when the code is accepted.

Observed behavior: when a code is rejected due to rules (e.g., minimum $100 spend but cart is $50), the code shows as rejected in checkout, yet the script still applies the subsequent discount logic as if it were accepted. This suggests rejected? may not reflect the final state in production or the script evaluation order differs from expectations.

Key questions:

  • How to ensure script logic only executes when the discount code is actually applied/accepted?
  • If that’s not reliable, how to enforce constraints like “once per customer” within Scripts?

Status: No resolution yet; code snippet and the min-spend example are central to understanding the issue. Discussion remains open.

Summarized with AI on January 12. AI used: gpt-5.

I have this line at the top of my code:

if Input.cart.discount_code&.code == "CYBER2023" && !Input.cart.discount_code&.rejected?

However, if the code is rejected (like it has a minimum of $100 and the cart is $50), it rejects the code but applies everything that comes after the if statement above.

So, I have 2 questions:

  • How can I make scripts not work if the code is not applied? (In production it simply passes that the code is not rejected even if it is)
  • If that’s not possible, how do I implement features like the discount only being used once per customer?