Articles on: Discounts

How do I apply a discount to a checkout upsell?‍

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")
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")
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.

Updated on: 29/11/2022

Was this article helpful?

Share your feedback

Cancel

Thank you!