Disable all discount except 1 discount code

Topic summary

A Shopify store owner needs to temporarily disable all discount codes except one specific code, while keeping automatic discounts and gift cards functional.

Solution Provided:
PaulNewton suggests modifying the DisableDiscountCodesCampaign script by adding a conditional return statement:

  • return if cart.discount_code.code == "unblock-code" to whitelist a specific discount code
  • This prevents the rejection logic from running for the allowed code

Confirmed Working:
The original poster confirms the solution works correctly and shares the complete working code. Gift cards are verified to remain unaffected since they’re treated as products during checkout, not discounts.

Additional Use Cases:

  • For allowing codes with specific prefixes (e.g., loyalty coupons), use .start_with? method instead of .include? to avoid false positives
  • Example: return if cart.discount_code.code.start_with? "YOUR_PREFIX"

Note: This solution requires Shopify Plus (Script Editor access). A non-Plus merchant asks about alternatives for temporarily disabling discount code entry during Black Friday sales without manually toggling thousands of codes, but no solution is provided for that scenario.

Summarized with AI on November 14. AI used: claude-sonnet-4-5-20250929.

Hi,

I need to disable all the discounts except 1 discount code for a limited time.

https://help.shopify.com/en/manual/checkout-settings/script-editor/examples/line-item-scripts#disable-discount-codes

Sample code removes all discounts. But I need

  • Disable all discounts except 1 code.
  • Shopify Automatic discounts should be working
  • Gift cards should be working

Thank you!

REJECTION_MESSAGE = 'Discount codes cannot be used during this sale'

# ================================ Script Code (do not edit) ================================
# ================================================================
# DisableDiscountCodesCampaign
#
# Any discount codes will be rejected with the entered message.
# ================================================================
class DisableDiscountCodesCampaign
  def initialize(rejection_message)
    @rejection_message = rejection_message
  end

  def run(cart)
    return if cart.discount_code.nil?

    cart.discount_code.reject(message: @rejection_message)
  end
end

CAMPAIGNS = [
  DisableDiscountCodesCampaign.new(REJECTION_MESSAGE),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart

@Nalinda you’d want to short circuit either by returning early in the DisableDiscountCodesCampaign run() method

return if cart.discount_code.code == "discountcode"

or skipping the current loop in the CAMPAIGNS.each loop

next if cart.discount_code.code == "discountcode"

That’s the quick fix example and may need to be adjusted to more specifically loop of all discounts codes.

And you’d want to change the DisableDiscountCodesCampaign class to accept a parameter of specific discount or list of exclusions for better code quality and longterm use

1 Like

Awesome, Thanks a lot @PaulNewton
It is working correctly.

Just want to confirm, This does not block adding gift cards right?

Thank you again for the big help!

Working code, if anyone needs it

REJECTION_MESSAGE = 'Discount codes cannot be used during this sale'

class DisableDiscountCodesCampaign
  def initialize(rejection_message)
    @rejection_message = rejection_message
  end

  def run(cart)
    return if cart.discount_code.nil?
    
    return if cart.discount_code.code == "unblock-code"

    cart.discount_code.reject(message: @rejection_message)
  end
end

CAMPAIGNS = [
  DisableDiscountCodesCampaign.new(REJECTION_MESSAGE),
]

CAMPAIGNS.each do |campaign|
  campaign.run(Input.cart)
end

Output.cart = Input.cart

Afaik this should never block gift cards as they aren’t just discounts they are treated as products at least during checkout then as currency for redemption.

Not even sure if scripts can block gift cards but I’ve never tested that and it shouldn’t occur as a side effect anyway /shrug.

But you should still validate these types of concerns with actual test purchases.

Thank you, yes gift cards are working :slightly_smiling_face:

How would you adjust this to allow discount codes with a certain prefix? For example, I have loyalty coupons generated with a 3 letter prefix. I’d like those to still work.

//Include

return if cart.discount_code.code.include? “YOUR_PREFIX”

//Prefix only

return if cart.discount_code.code.start_with?(“YOUR_PREFIX”)

Use the .start_with method for a prefix approach like this when there’s few characters. Otherwise it risks false positives with the short prefix being INSIDE the full string i.e a discount code prefix “red” would match “hundred” using .include? “red”

Hi Nalinda,

Where would you copy this code to if you are not on a Shopify plus plan? We have thousands of automatic discount codes that generate through Klavio and other apps plus influencers codes etc.

For Black Friday we are automatically applying discounts by changing the prices on all listings and want to either disable the coupon code box or put in some code that does not allow codes to be used. It is only for the BCFM weekend and then on Tuesday back to business as usual so I don’t want to turn off and on thousands of codes.

Thanks!

You can use the PromoLock app for this.