Articles on: Discounts

How to remove discounts on upsell products when the conditions for the upsell offer are no longer met

When offering upsells, you might be offering an exclusive product or discount based on certain cart conditions. To avoid that shoppers add these discounted products to cart in checkout and then modify their cart, we can use a Shopify Script. It will remove the discount from the upsell product once the conditions are no longer met.

You can achieve this by adding your version of the following code to your Shopify Scripts when using UpsellPlus.

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


You'll need to replace

REQUIRED_PRODUCT_ID = 'X' with the product ID of a product that needs to be in cart to receive the upsell offer

MINIMUM_CART_VAL = 'X' with the minimum cart value

And the '0.60' with the discount you want to apply

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.

Updated on: 18/04/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!