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

DiscountConfig = Struct.new(:block_id, :percentage, :message) do

def applicable?(line_item)

line_item.properties["_checkoutblocks"] == block_id

end


def apply_discount(line_item)

return unless applicable?(line_item)

discount_amount = line_item.line_price * percentage

new_price = line_item.line_price - discount_amount

line_item.change_line_price(new_price, message: message)

end

end


# Define multiple discount configurations

discount_configs = [

DiscountConfig.new("631f8edae5f9ce785673920c", 0.1, "Checkout offer 1"),

# Add more configurations as needed

]


Input.cart.line_items.each do |line_item|

discount_configs.each { |config| config.apply_discount(line_item) }

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

Multiple blocks with discounts

You can modify the example script to accommodate multiple discounts at the same time: https://gist.github.com/gil--/4a5e920c62c975b75ec886377586fb13. Make sure to change the block ids, discount, and message to suit your needs.

DiscountConfig = Struct.new(:block_id, :percentage, :message) do

def applicable?(line_item)

line_item.properties["_checkoutblocks"] == block_id

end


def apply_discount(line_item)

return unless applicable?(line_item)

discount_amount = line_item.line_price * percentage

new_price = line_item.line_price - discount_amount

line_item.change_line_price(new_price, message: message)

end

end


# Define multiple discount configurations

discount_configs = [

DiscountConfig.new("640f512c6251038ecacb8fa7", 0.1, "Checkout offer 1"),

DiscountConfig.new("some_other_id", 0.15, "Checkout offer 2"),

# Add more configurations as needed

]


Input.cart.line_items.each do |line_item|

discount_configs.each { |config| config.apply_discount(line_item) }

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.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us