Created
November 15, 2021 16:17
-
-
Save martinsoender/707862ed008996b69c2485cd1453c108 to your computer and use it in GitHub Desktop.
Tiered Discounts By Spending Threshold
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
| # Define spending thresholds, from lowest spend to highest spend. | |
| SPENDING_THRESHOLDS = [ | |
| { | |
| spend: 10000, # spend amount (in cents) | |
| discount: 15 # percentage discount | |
| }, | |
| { | |
| spend: 20000, | |
| discount: 20 | |
| }, | |
| { | |
| spend: 30000, | |
| discount: 30 | |
| } | |
| ] | |
| # Find any applicable spending threshold. | |
| eligible_threshold = nil | |
| SPENDING_THRESHOLDS.each do |threshold| | |
| if Input.cart.subtotal_price_was.cents >= threshold[:spend] | |
| eligible_threshold = threshold | |
| end | |
| end | |
| # Apply threshold. | |
| if !eligible_threshold.nil? | |
| Input.cart.line_items.each do |line_item| | |
| line_item.change_line_price(line_item.line_price * (1.0 - (eligible_threshold[:discount] / 100.0)), message: "#{eligible_threshold[:discount]}% off for purchases over $#{eligible_threshold[:spend] / 100}!") | |
| end | |
| end | |
| Output.cart = Input.cart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment