Disable all discount except 1 discount code

Solved
Nalinda
Shopify Partner
13 0 3

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
0 Likes
PaulNewton
Shopify Partner
2580 134 442

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

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes
Nalinda
Shopify Partner
13 0 3

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

 

 

0 Likes
PaulNewton
Shopify Partner
2580 134 442

@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.

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes
Nalinda
Shopify Partner
13 0 3

Thank you, yes gift cards are working  

0 Likes