Personalized checkout and custom promotions with Shopify Scripts
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
@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!
We recently spoke with Zopi developers @Zopi about how dropshipping businesses can enha...
By JasonH Oct 23, 2024A big shout out to all of the merchants who participated in our AMA with 2H Media: Holi...
By Jacqui Oct 21, 2024We want to take a moment to celebrate the incredible ways you all engage with the Shopi...
By JasonH Oct 15, 2024