Articles on: Discounts

How to set up tiered discounts with Script Editor

Overview



Offering tiered discounts encourages customers to purchase more by providing increasing incentives based on the quantity of items added to their cart. With Shopify's Script Editor, you can implement dynamic tiered discounts for upsell offers. To offer tiered discounts, you can use the Script Editor. This article will show how with the right script.

Scenario Example


Consider a multi-product upsell offer with three items. The desired incentives are:
Add 1 item: Customer receives $2 off.
Add 2 items: Customer receives $6 off.
Add 3 items: Customer receives $10 off.

These discounts adjust dynamically based on the number of upsell items added, eliminating the need to specify the discount amount within the offer itself. To promote this, you might use messaging like: "Get up to $10 OFF by adding all these products to your order."

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 Tiered Discount Script
Copy and paste the following script into the editor:

# 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


Explanation:


The script initializes a hash to count occurrences of each __upsell_id and iterates through the cart's line items to tally upsell items.

Based on the count, it applies the corresponding discount:
1 item: $2 off
2 items: $3 off per item ($6 total)
3 items: $3.33 off per item ($10 total)

IMPORTANT! We encourage you to test and place test orders first to confirm this script is compatible with your store


Key Tips
Testing: Always perform thorough testing to confirm compatibility with your store's setup.
Messaging: Clearly communicate the potential savings to customers to encourage them to add more items.

Related Articles
Intro to Discounts
How Do I Apply a Discount to a Checkout Upsell Using Script Editor?

By implementing tiered discounts using the Script Editor, you can incentivize customers to increase their purchases, thereby boosting your average order value.

Updated on: 14/02/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!