How to remove discounts on upsell products when the conditions for the upsell offer are no longer met with Script Editor
Overview
When offering upsell products with conditional discounts, it's crucial to ensure that these discounts are only applied when specific cart conditions are met. If a customer modifies their cart in a way that no longer satisfies these conditions, the discount should be removed. This can be achieved by implementing a script in Shopify's Script Editor that dynamically checks cart conditions and adjusts discounts accordingly.
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 Conditional Discount Script
Copy and paste the following script into the editor:
Remove the discount on a product added as an upsell if the cart item quantity conditions are no longer met
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and line_item.properties["__upsell_id"].include? "1567d4cf-4231-410a-bc56-3b64ff1a9a2a"
if Input.cart.line_items.length > 3
line_item.change_line_price(line_item.line_price * 0.0, message: "Discount")
end
end
end
Output.cart = Input.cart
end
Output.cart = Input.cart
In this example, the 100% discount will be removed when the cart item quantity falls below 3 (if Input.cart.line_items.length > 3) . This can be effective in a 'Buy 2 Get 2' upsell strategy.
Remove an upsell product if the minimum cart value conditions to receive the upsell offer are no longer met
REQUIRED_PRODUCT_ID = 42385195663572 #LITTLE HELPER
MINIMUM_CART_VAL = 60 #DOLLARS TOTAL CART VALUE NECESSARY FOR UPSELLING
upsell_allowed = false
#CHECK IF CONDITIONS ARE MET
Input.cart.line_items.each do |line_item|
if line_item.variant.id == REQUIRED_PRODUCT_ID and Input.cart.subtotal_price_was > (Money.new(cents:100) * MINIMUM_CART_VAL)
upsell_allowed = true
end
end
#DELETE ITEM FROM CART IF CONDITIONS ARE NOT MET
Input.cart.line_items.reject! { |line_item| line_item.properties.key?("__upsell_id") and upsell_allowed == false }
Output.cart = Input.cart
Remove a discounted upsell product if the minimum cart value conditions to receive the upsell offer are no longer met
REQUIRED_PRODUCT_ID = 42385195663572 #LITTLE HELPER
MINIMUM_CART_VAL = 60 #DOLLARS TOTAL CART VALUE NECESSARY FOR UPSELLING
upsell_allowed = false
#CHECK IF CONDITIONS ARE MET
Input.cart.line_items.each do |line_item|
if line_item.variant.id == REQUIRED_PRODUCT_ID and Input.cart.subtotal_price_was > (Money.new(cents:100) * MINIMUM_CART_VAL)
upsell_allowed = true
end
end
#APPLY DISCOUNT FOR UPSELLING ITEM
Input.cart.line_items.each do |line_item|
if line_item.properties.has_key?("__upsell_id") and upsell_allowed
line_item.change_line_price(line_item.line_price * 0.60, message: "Upsell Discount")
end
end
#DELETE ITEM FROM CART IF CONDITIONS ARE NOT MET
Input.cart.line_items.reject! { |line_item| line_item.properties.key?("__upsell_id") and upsell_allowed == false }
Output.cart = Input.cart
Parameters to Customize:
Offer ID: Replace 1567d4cf-4231-410a-bc56-3b64ff1a9a2a with the specific Offer ID of your upsell product.
Minimum Cart Value: Replace 5000 with the minimum cart value (in cents) required for the upsell discount to apply. For example, 7500 would require a $75 minimum cart total.
Discount Rate: Adjust 0.60 to set the discount percentage. For example: 0.80 = 20% off, 0.70 = 30% off, 0.60 = 40% off etc.
This script will only add the discount if the required product is in cart, a minimum cart value is met. If the conditions are no longer met, the discounted product will be removed.
Key Considerations
Testing: Always perform thorough testing to confirm that discounts are applied and removed correctly based on the specified conditions.
Script Management: If you have multiple scripts, ensure they do not conflict with each other.
Performance: Keep your scripts optimized to prevent any potential impact on the checkout process.
Related Articles
How to Block Manual Discount Codes from Being Added on Top of an Automatic Script Discount Using Script Editor
Intro to Discounts
Updated on: 14/02/2025
Thank you!