How to set up tiered discounts with Script Editor
To offer tiered discounts, you can use the Script Editor. This article will show how with the right script.
As an example, we will have this scenario with a multi-product upsell offer in which there are three items to be upsold. The goal is to cross-sell all three items of the offer by offering these incentives:
If one item from the offer is added to the order, your customer will get $2 off
If two items from the offer are added to the order, your customer will get $6 off
If all three items from the offer are added to the order, your customer will get $10 off
Because these discounts will be dynamic, you won't have to specify the discount amount on the offer itself as that will be taken care by the script. Instead, you may want to customize your subtitles to encourage people to get the promotion with strings such as: ''Get up to XX OFF by adding all these products to your order"
If one product is added, that item will have a $2 discount
If two products are added, each item will have a $3 discount - (It will add up to $6 in total)
If all three products are added, each item will have a $3.3 discount - (This will add up to $10 in total)
IMPORTANT! We encourage you to test and place test orders first to confirm this script is compatible with your store
As an example, we will have this scenario with a multi-product upsell offer in which there are three items to be upsold. The goal is to cross-sell all three items of the offer by offering these incentives:
If one item from the offer is added to the order, your customer will get $2 off
If two items from the offer are added to the order, your customer will get $6 off
If all three items from the offer are added to the order, your customer will get $10 off
Because these discounts will be dynamic, you won't have to specify the discount amount on the offer itself as that will be taken care by the script. Instead, you may want to customize your subtitles to encourage people to get the promotion with strings such as: ''Get up to XX OFF by adding all these products to your order"
What is the script I should use?
# Initialize a hash to store counts for each upsell_id
upsell_id_counts = Hash.new(0)
# Iterate through line items and count the occurrences of each upsell_id
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id_counts[line_item.properties["__upsell_id"]] += 1
end
end
# Iterate through line items again and apply a $2 discount only if there is exactly one upsell
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id = line_item.properties["__upsell_id"]
if upsell_id_counts[upsell_id] == 1
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 200), message: "Upsell Discount")
end
end
end
Output.cart = Input.cart
# Initialize a hash to store counts for each upsell_id
upsell_id_counts = Hash.new(0)
# Iterate through line items and count the occurrences of each upsell_id
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id_counts[line_item.properties["__upsell_id"]] += 1
end
end
# Iterate through line items again and apply a $6 discount only if there are exactly two upsells
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id = line_item.properties["__upsell_id"]
if upsell_id_counts[upsell_id] == 2
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 300), message: "Upsell Discount")
end
end
end
Output.cart = Input.cart
# Initialize a hash to store counts for each upsell_id
upsell_id_counts = Hash.new(0)
# Iterate through line items and count the occurrences of each upsell_id
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id_counts[line_item.properties["__upsell_id"]] += 1
end
end
# Iterate through line items again and apply a $10 discount only if there are exactly three upsells
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id")
upsell_id = line_item.properties["__upsell_id"]
if upsell_id_counts[upsell_id] == 3
line_item.change_line_price(line_item.line_price - Money.derived_from_presentment(customer_cents: 333), message: "Upsell Discount")
end
end
end
Output.cart = Input.cart
This is how the script works:
If one product is added, that item will have a $2 discount
If two products are added, each item will have a $3 discount - (It will add up to $6 in total)
If all three products are added, each item will have a $3.3 discount - (This will add up to $10 in total)
IMPORTANT! We encourage you to test and place test orders first to confirm this script is compatible with your store
Updated on: 23/07/2024
Thank you!