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.



To achieve this, we'll add a Shopify Script in 3 steps. No need for an engineer!

Step 1: ​​Go to the official Shopify Script Editor App (https://apps.shopify.com/script-editor)‍
Click ‘Create Script’ > Choose the type of script you’re adding. For this example we'll choose between a percentage (%) off a product or amount ($) off a product discount. Select one and click ‘Create Script’. Name your Script.‍

Step 2: Add the script
Now we’ll add a small code snippet.

For a % off discount (10% in this example):
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 $ amount off discount ($5 in this example):
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.‍

Step 3: Save and publish the script
Click 'Save and publish' in the script editor. Your discount is now live.

Customizing the discount
In this script, you can control:

1) The $ or % of the discount:

%: line_item.line_price * 0.90 = show product at 90% of it's price
$: line_item.line_price - Money.derived_from_presentment(customer_cents: 500) = 500 cents, so enter the cents you want to discount the product

2) The upsell message: Replace the "Upsell Discount" with your preferred discount code

If you already have existing campaigns running with Shopify Scripts, you can add this script just below it.

3) The offer ID, which will target a specific UpsellPlus offer you've configured.

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

Updated on: 17/09/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!