Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dylanjhunt/d8e57b1090876d222235719fbff90dcf to your computer and use it in GitHub Desktop.
Save dylanjhunt/d8e57b1090876d222235719fbff90dcf to your computer and use it in GitHub Desktop.
DISCOUNTS_BY_QUANTITY = {
10_000 => 20,
1_000 => 15,
100 => 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 buying at least #{quantity}."
line_item.change_line_price(
line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
message: message,
)
end
Output.cart = Input.cart
@vgcrepairs
Copy link

how would I do this by a specific tag "discountable"

@robizzt
Copy link

robizzt commented Mar 2, 2021

What if I have 2 variants of same product?
I want to give discount if I have 1 variant x 2 quantity, or 2 variant x 1 quantity.
Ex. This only works if you get 2 blue variants of same t-shirt, not when you buy 1 red and 1 blue variants of same t-shirt.

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