Skip to content

Instantly share code, notes, and snippets.

@seedcms
Created January 22, 2017 18:34
Show Gist options
  • Save seedcms/3718a05b149c97a1a3ddda68ae273bfa to your computer and use it in GitHub Desktop.
Save seedcms/3718a05b149c97a1a3ddda68ae273bfa to your computer and use it in GitHub Desktop.
Quantity Breaks
DISCOUNTS_BY_QUANTITY = {
100 => 20,
50 => 15,
25 => 10,
10 => 5,
}
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
line_item.quantity >= quantity
end
next unless discount
message = "#{discount}% off when you buy #{quantity} items."
line_item.change_line_price(
line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
message: message,
)
end
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment