How to add a discount to upsells
Checkout Blocks supports showing and enabling discounted products when added from a Checkout Block such as checkout upsells and checkbox upsells.
How to add discounts to upsell blocks
When using blocks within checkout, you can leverage the Shopify Script Editor app (direct link: https://script-editor.shopifyapps.com) to add discounts to products added via a checkout block. Please note that discounting behavior works differently for post purchase blocks.
Simply target any line item based on the following property:
Key: _checkoutblocks
Value: Your Block ID
Example Script
The following example discounts upsell products by 10% when added from the Checkout Block 631f8edae5f9ce785673920c (Make sure you update the BLOCK_ID and DISCOUNT to meet your needs).
The following script can be found here: https://gist.github.com/gil--/4bf3463a2a1401acea53a8bd024d0845
BLOCK_ID = "631f8edae5f9ce785673920c" # Replace with block id
DISCOUNT = 0.10
DISCOUNT_MESSAGE = "Checkout offer"
Input.cart.line_items.each do |line_item|
next unless line_item.properties.has_key?("_checkoutblocks")
next unless line_item.properties.has_value?(BLOCK_ID)
DISCOUNT_AMOUNT = line_item.line_price * DISCOUNT
line_item.change_line_price(line_item.line_price - DISCOUNT_AMOUNT, message: DISCOUNT_MESSAGE)
end
Output.cart = Input.cart
Remove discount when only upsells are in the cart
The following is an example Shopify script that prevents adding the upsell discount when only products that were added from Checkout Blocks are in the cart (Make sure you update the BLOCK_ID and DISCOUNT to meet your needs).
The following script can be found here: https://gist.github.com/gil--/4bf3463a2a1401acea53a8bd024d0845
BLOCK_ID = "640f512c6251038ecacb8fa7" # Replace with block id
DISCOUNT = 0.10
DISCOUNT_MESSAGE = "Checkout offer"
HAS_NORMAL_PRODUCT = false
Input.cart.line_items.each do |line_item|
next unless !line_item.properties.has_key?("_checkoutblocks")
HAS_NORMAL_PRODUCT = true
break
end
Input.cart.line_items.each do |line_item|
next unless HAS_NORMAL_PRODUCT
next unless line_item.properties.has_key?("_checkoutblocks")
next unless line_item.properties.has_value?(BLOCK_ID)
DISCOUNT_AMOUNT = line_item.line_price * DISCOUNT
line_item.change_line_price(line_item.line_price - DISCOUNT_AMOUNT, message: DISCOUNT_MESSAGE)
end
Output.cart = Input.cart
Limitations
- The discount has the same limitations as any discount that uses Shopify scripts.
- You may want to add a quantity check to prevent abuse from customers changing the quantity in cart.
Why do you rely on Shopify scripts for the discount?
Shopify has announced that by August 2024, Shopify scripts will be deprecated with Shopify unctions replacing it. However, Shopify functions does not currently support line-item based discounts and so we are forced to leverage Shopify scripts until Shopify provides similar functionality on Shopify functions.