How to block manual discount codes from being added on top of an automatic script discount using Script Editor
Overview
When offering automatic discounts on upsell products, it's essential to prevent customers from stacking additional manual discount codes, which could erode profit margins. By implementing a script in Shopify's Script Editor, you can ensure that once an automatic discount is applied to an upsell item, any additional manual discount codes entered by the customer are rejected.
If you want to offer an automatic discount on a checkout upsell, but don't want an additional discount to be added in the 'Discount code' field, the script below will help.
Step-by-Step Guide
Log in to Shopify Admin
Access your Shopify Admin panel using your credentials.
Install and Open the Script Editor App
If not already installed, download the Script Editor app from the Shopify App Store.
Open the Script Editor from your list of installed apps.
Create a New Script
Click on Create Script.
Choose the Line Item script type.
Insert the Discount and Rejection Script
Copy and paste the following script into the editor:
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "54ea7069-5dec-4fbe-a50f-aff10f071636"
line_item.change_line_price(line_item.line_price * 0.7, message: "30% OFF")
if Input.cart.discount_code
Input.cart.discount_code.reject(message: "Discount already applied")
end
end
end
Output.cart = Input.cart
This script will block additional discounts when an upsell with the specified offer ID (54ea7069-5dec-4fbe-a50f-aff10f071636) is added to cart.
Parameters to Customize:
Offer ID: Replace 54ea7069-5dec-4fbe-a50f-aff10f071636 with the specific Offer ID of your upsell product.
Discount Rate: Adjust 0.70 to reflect your desired discount. For example, use 0.80 for a 20% discount.
Discount Message: Change "30% OFF" to a message that aligns with your branding, such as "Exclusive Offer" or "Special Deal".
Rejection Message: Customize "Discount already applied" to inform customers why their manual discount code was rejected.
Save and Publish the Script
Click Save to apply the script.
Applying to Multiple Upsell Offers
If you have multiple upsell offers and want to prevent manual discount codes from being applied to any of them, you can use the following script structure:
def apply_discount(line_item, upsell_id, discount_factor, discount_message)
return unless line_item.properties.has_key?("__upsell_id") && line_item.properties["__upsell_id"].include?(upsell_id)
line_item.change_line_price(line_item.line_price * discount_factor, message: discount_message)
if Input.cart.discount_code
Input.cart.discount_code.reject(
message: "Discount already applied"
)
end
end
Input.cart.line_items.each do |line_item|
apply_discount(line_item, "03e7fe18-fe02-44bc-9517-a0c7a0ea935e", 0.7, "30% OFF")
apply_discount(line_item, "cf568376-31ee-4c54-8fcf-ab5efa1507c9", 0.7 "30% OFF")
apply_discount(line_item, "66ca6ee1-de37-4ff3-969b-8f2eb5aa2397", 0.7 "30% OFF")
apply_discount(line_item, "cb07b47f-f69d-45ad-8941-eeff08491cd6", 0.77, "30% OFF")
apply_discount(line_item, "612fa215-fb65-4832-9355-154f1529c76d", 0.7, "30% OFF")
apply_discount(line_item, "f4ca1e53-f489-4e31-8ff3-29c901b37eba", 0.7, "30% OFF")
apply_discount(line_item, "b78a2517-3513-41c6-a428-dd656d29734e", 0.7 "30% OFF")
apply_discount(line_item, "54ea7069-5dec-4fbe-a50f-aff10f071636", 0.7, "30% OFF")
end
Output.cart = Input.cart
Parameters to Customize:
Offer IDs: Replace 03e7fe18-fe02-44bc-9517-a0c7a0ea935e and other ID placeholders with the respective Offer IDs of your upsell products.
Discount Factors: Adjust 0.70, 0.77, and other decimals to reflect your desired discounts for each upsell offer.
Discount Messages: Customize "30% OFF" to messages that align with your branding.
Key Considerations
Testing: Always perform thorough testing to confirm that the automatic discount is applied correctly and that manual discount codes are appropriately rejected.
Customer Communication: Ensure that the rejection message clearly communicates to customers why their manual discount code was not accepted, to prevent confusion or dissatisfaction.
Related Articles
Intro to Discounts
How to Apply a Discount to a Checkout Upsell Using Script Editor
Updated on: 14/02/2025
Thank you!