Skip to content

Instantly share code, notes, and snippets.

@gil--
Last active December 7, 2023 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gil--/4bf3463a2a1401acea53a8bd024d0845 to your computer and use it in GitHub Desktop.
Save gil--/4bf3463a2a1401acea53a8bd024d0845 to your computer and use it in GitHub Desktop.
BLOCK_ID = "640f512c6251038ecacb8fa7" # Replace with block id
DISCOUNT = 0.10 # Provides a 10% discount
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
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"), # BLOCK_ID, discount (10%), message
DiscountConfig.new("BLOCK_ID", 0.15, "Checkout offer 2"), # BLOCK_ID, discount (15%), message
# Add more block discounts as needed
]
Input.cart.line_items.each do |line_item|
discount_configs.each { |config| config.apply_discount(line_item) }
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment