Last active
October 24, 2018 22:38
-
-
Save vishalarora91/c84f19685a9ee16ad5af93496d1c265f to your computer and use it in GitHub Desktop.
Percentage off least expensive item / Buy One Get One Free - Shopify Scripts
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
| DISCOUNT_AMOUNT = 100 | |
| # Counting quantity of products | |
| $cartcount = 0 | |
| Input.cart.line_items.each do |item| | |
| $cartcount = $cartcount + item.quantity | |
| end | |
| # Offering discount based on the quantity after sorting the array for getting the least expensive item | |
| if ($cartcount > 1) | |
| line_item = Input.cart.line_items.sort_by { |line_item| line_item.variant.price }.first | |
| if line_item.quantity > 1 | |
| line_item = line_item.split(take: 1) | |
| Input.cart.line_items << line_item | |
| end | |
| line_item.change_line_price(line_item.line_price * (1.0 - (DISCOUNT_AMOUNT / 100.0)), message: "Buy one Get one free!") | |
| end | |
| Output.cart = Input.cart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment