New Shopify Certification now available: Liquid Storefronts for Theme Developers

Disable all discount except 1 discount code

Solved
Nalinda
Shopify Partner
30 0 12

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#disabl...

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
Accepted Solution (1)
PaulNewton
Shopify Partner
5931 537 1241

This is an accepted solution.

@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

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


View solution in original post

Replies 8 (8)
PaulNewton
Shopify Partner
5931 537 1241

This is an accepted solution.

@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

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


Nalinda
Shopify Partner
30 0 12

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

 

 

PaulNewton
Shopify Partner
5931 537 1241

@Nalinda wrote:

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


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.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


Nalinda
Shopify Partner
30 0 12

Thank you, yes gift cards are working 🙂 

StandardSqueeze
Not applicable
1 0 0

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!

kylemoloo
Shopify Partner
13 0 6

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.

Kyle Moloo | Sr. Director of Ecommerce
Jeesuz
Shopify Partner
2 0 0

//Include

 

return if cart.discount_code.code.include? "YOUR_PREFIX"

 

//Prefix only

 

return if cart.discount_code.code.start_with?("YOUR_PREFIX")

PaulNewton
Shopify Partner
5931 537 1241

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"

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org