How do I only discount a product when it's added as an upsell with Script Editor?
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.
Go to your Shopify Script Editor app and add the following script for
A percentage discount:
A discount amount:
There are three parameters you can adjust. They are highlighted in green.
The discount % or discount $
Example of a 20% discount
line_item.change_line_price(line_item.line_price * 0.8)
Example of a $5 discount
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 500))
The discount code displayed to the customers
Example of a discount code saying: Flash Sale
line_item.change_line_price(line_item.line_price * 0.8), message: "Flash Sale"
The specific upsell offer you're targeting with this script
Example of a discount targeting upsell offer with offer ID: e766cbff-060d-4ed7-9797-ccb9d2bdcf1a
if line_item.properties.has_key?("__upsell_id") && line_item.properties["__upsell_id"].include? "e766cbff-060d-4ed7-9797-ccb9d2bdcf1a"
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.
Copy and paste it into the script.
Save and publish your script in the Script Editor app.****
Go to your Shopify Script Editor app and add the following script for
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
There are three parameters you can adjust. They are highlighted in green.
The discount % or discount $
Example of a 20% discount
line_item.change_line_price(line_item.line_price * 0.8)
Example of a $5 discount
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 500))
The discount code displayed to the customers
Example of a discount code saying: Flash Sale
line_item.change_line_price(line_item.line_price * 0.8), message: "Flash Sale"
The specific upsell offer you're targeting with this script
Example of a discount targeting upsell offer with offer ID: e766cbff-060d-4ed7-9797-ccb9d2bdcf1a
if line_item.properties.has_key?("__upsell_id") && line_item.properties["__upsell_id"].include? "e766cbff-060d-4ed7-9797-ccb9d2bdcf1a"
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.
Copy and paste it into the script.
Save and publish your script in the Script Editor app.****
Updated on: 23/07/2024
Thank you!