-
-
Save gil--/4bf3463a2a1401acea53a8bd024d0845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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