Skip to content

Instantly share code, notes, and snippets.

@martinsoender
Created November 15, 2021 16:17
Show Gist options
  • Save martinsoender/707862ed008996b69c2485cd1453c108 to your computer and use it in GitHub Desktop.
Save martinsoender/707862ed008996b69c2485cd1453c108 to your computer and use it in GitHub Desktop.
Tiered Discounts By Spending Threshold
# 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