Created
November 10, 2023 13:10
-
-
Save fearless-spider/d6bce805060d75ef7c30d1e21a383337 to your computer and use it in GitHub Desktop.
Fixed Bundle Discount
This file contains hidden or 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
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Buy X products for Y total price