Personalized checkout and custom promotions with Shopify Scripts
Hi,
I need to disable all the discounts except 1 discount code for a limited time.
Sample code removes all discounts. But I need
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
Solved! Go to the solution
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
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
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
@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
Thank you, yes gift cards are working 🙂
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!
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"
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
User | RANK |
---|---|
2 | |
2 | |
2 | |
2 | |
2 |
Transform this holiday season into a shopping spree. Plus, learn how to effortlessly open ...
By Jasonh Dec 8, 2023Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023