Skip to content

Instantly share code, notes, and snippets.

@fearless-spider
Created November 10, 2023 13:10
Show Gist options
  • Select an option

  • Save fearless-spider/d6bce805060d75ef7c30d1e21a383337 to your computer and use it in GitHub Desktop.

Select an option

Save fearless-spider/d6bce805060d75ef7c30d1e21a383337 to your computer and use it in GitHub Desktop.
Fixed Bundle Discount
DISCOUNTS_BY_QUANTITY = {
8 => 275.00,
4 => 150.00,
}
if Input.cart.line_items.length == 4 || Input.cart.line_items.length == 8
discount = Input.cart.subtotal_price - (Money.new(cents: 100) * DISCOUNTS_BY_QUANTITY[Input.cart.line_items.length])
Input.cart.line_items.each do |line_item|
next line_item.properties.has_key?("_bundle")
new_line_price = 0
discount_after_item_disc = discount - line_item.variant.price
if discount_after_item_disc >= 0 * Money.new(cents: 100)
new_line_price = line_item.variant.price - line_item.variant.price
else
new_line_price = line_item.variant.price - discount
end
if discount >= 0 * Money.new(cents: 100)
discount = discount_after_item_disc
line_item.change_line_price(new_line_price, message: { message: "Bundle" })
end
end
end
Output.cart = Input.cart
@fearless-spider
Copy link
Author

Buy X products for Y total price

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment