Articles on: Discounts

How do I only discount a product when it's added as an upsell with Script Editor?

Overview



To ensure that a discount is applied exclusively when a product is added through an upsell offer, you can implement a Shopify Script that targets items with specific upsell identifiers. This method ensures that the discount is not applied if the product is added to the cart through other means.

When you want a discount to apply to a product only when it's added as an upsell, you can use a simple Shopify Script.

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:

A percentage discount:

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
end
Output.cart = Input.cart


A discount amount:

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: "Flash Sale"))
end
end
Output.cart = Input.cart


Parameters to Customize:


Discount Percentage: Adjust 0.80 to reflect the desired discount. For example, use 0.90 for a 10% discount.
Discount Amount: Adjust 500 to reflect the desired discount. For example, use 250 for a $5 discount.
Discount Message: Change Flash Sale to the message you want customers to see, such as Special Offer or Exclusive Deal.
Offer ID: Replace e766cbff-060d-4ed7-9797-ccb9d2bdcf1a with the specific offer ID from your UpsellPlus offer.

You can find the offer ID in the UpsellPlus app. Go to your upsell offer. At the top you will see the offer ID displayed.

offer ID in the app admin

Save and Publish your script.

Key Tips
Testing: Always perform thorough testing to confirm that the discount applies only when the product is added via the upsell offer.
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!