Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ashukasma/ff1d1ca275139cfed6dc13c274dd33a2 to your computer and use it in GitHub Desktop.
Save ashukasma/ff1d1ca275139cfed6dc13c274dd33a2 to your computer and use it in GitHub Desktop.
Shopify Scripts - Tiered Pricing
DISCOUNTS_BY_QUANTITY = {
4 => 20,
3 => 15,
2 => 10
}
discunted_count = 0
Input.cart.line_items.each do |line_item|
if line_item.variant.product.tags.include?('DiscountTag')
discunted_count = discunted_count + line_item.quantity
end
end
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
discunted_count == 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
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
@Mess69
Copy link

Mess69 commented Apr 15, 2018

Hello dear,
Can you tell me where to put this code for quantity break?
Thank you

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