Shopify Scripts and App Created Discount Codes

Jim_Zafrani
Shopify Partner
8 0 1

 

I have a script that allows for discounts to be stacked on top of our discounts. The use case is that we have a generic code that we give to VIPs that should be allowed to use on top of any automatic discount that gets applied to a product.

The issue is that the script works perfectly for discount codes configured by the admin interface but does not work for any code generated by an app. We use a popup that automatically assigns a discount code to every new customer and those codes are discarded after this script runs.

Are app created discounts handled differently in Shopify Scripts?

The script is as follows:

# Remove disallowed items. We check again in the campaign, this is just optimization
available = cart.line_items.select { |item| !Eligibility::disallowed?(item) }
# Discount items one campaign at a time, remove items for later campaigns that
# have already been discounted
@campaigns.each do |campaign|
discounted = campaign.run(available, cart.discount_code)
if (discounted)
available = self.remove_discounted(discounted, available)
end
end
# if there is a discount code, discount any remaining eligible items
# if this is a fixed price discount, we will just leave it on the cart.
# If this is a stacking discount, then we go through all the items rather
# than just those that have been excluded from campaigns so far.
if (
cart.discount_code &&
cart.discount_code.instance_of?(CartDiscount::Percentage)
)
if (STACKING_DISCOUNT_CODES.any? {|code| code.downcase == cart.discount_code.code.downcase })
cart.discount_code.reject(message: DISALLOWED_MESSAGE)
self.apply_discount_stacking(cart.line_items, cart.discount_code)
else
if (available.length() > 0)
cart.discount_code.reject(message: DISALLOWED_MESSAGE)
self.apply_discount_to_remaining(available, cart.discount_code)
end
end
end
return cart

Reply 1 (1)

RobDukarski
Shopify Partner
88 15 20

@Jim_Zafrani 

Discount codes should work the same in the Script Editor app across the board.

My recommendation is to tag your group of VIP customers with a specific tag and then target those customers specifically in the script if you want to allow the automatic discounts to stack with any discount code applied only to those VIP customers. If you are not already limiting the discount code to a specific customer group in the Shopify admin back-end then anybody could potentially use it to stack discounts.

With that you can make sure that any discount code a non-VIP customer enters is rejected if there is an automatic discount previously applied.

The setup should be fairly simple with a single conditional at the end of the script as your automatic discounts are priority in your scenario.

Hope that helps!

- Rob Dukarski
Helping to make commerce better for everyone!