Articles on: Discounts

How do I apply a discount to a checkout upsell using Script Editor?‍

If you don't want to add a script and would like UpsellPlus to create the discount automatically, refer to the first section of Intro to discounts and use Shopify Functions instead.


Overview



Applying discounts to upsell products during checkout can incentivize customers to add more items to their purchase, thereby increasing your average order value. By utilizing Shopify's Script Editor, you can create scripts that automatically apply discounts to specific upsell offers.

To achieve this, we'll add a Shopify Script in just a few steps - no need for an engineer!

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 Script
Copy and paste the following scripts into the editor:

For this example we'll choose between a percentage (%) off a product or amount ($) off a product discount.

For a Percentage Discount (e.g., 10% off):
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "e766cbff-060d-4ed7-9797-ccb9d2bdcf1a"
line_item.change_line_price(line_item.line_price * 0.90, message: "Upsell Discount")
end
end
Output.cart = Input.cart


For a Fixed Amount Discount (e.g., $5 off):
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "e766cbff-060d-4ed7-9797-ccb9d2bdcf1a"
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 500), message: "Upsell Discount")
end
end
Output.cart = Input.cart


If you’re not familiar with code, this will do the following: ‍

IF the item added to the cart has “upsell_id” as a property
THEN apply a discount and display the message ‘Upsell Discount’‍ for the discount code when this product is added to cart

The “upsell_id” is added by the app to all items that get upsold, so we can track your upsell analytics.‍

Save and Publish in Script Editor to make your discounts live.

Customizing the Discount



In this script, the parameters you can control are as follows:

Offer ID: Replace the e766cbff-060d-4ed7-9797-ccb9d2bdcf1a placeholder with the specific Offer ID from your UpsellPlus offer.
Discount Value: Adjust 0.90 to reflect the desired percentage discount (e.g., 0.85 for 15% off). For fixed discounts, change 500 to the desired amount in cents (e.g., 1000 for $10 off).
Discount Message: Customize "Upsell Discount" to the message you want customers to see, such as "Special Offer" or "Exclusive Deal".

You can find it in the app when you click into your upsell:



Shopify sets a limit on the number of Line Item scripts you can have live. If you want to combine multiple discounts into 1 script, you can use this threaded template:

Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "e766cbff-060d-4ed7-9797-ccb9d2bdcf1a"
line_item.change_line_price(line_item.line_price * 0.8, message: "Flash Sale")
end
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "ADDOFFERID"
line_item.change_line_price(line_item.line_price * 0.5, message: "Special Discount")
end
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "ADDOFFERID"
line_item.change_line_price(line_item.line_price * 0.4, message: "Promotion")
end
end
Output.cart = Input.cart


Key Tips
Testing: Always perform thorough testing to confirm that discounts are applied correctly to the intended upsell offers.
Script Management: If you have multiple scripts, ensure they do not conflict with each other.

Related Articles
Intro to Discounts
How to Target Specific Offers with a Discount Using the Offer ID with Script Editor

Updated on: 14/02/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!