Class: SolidusPromotions::Calculators::TieredFlatRate
- Inherits:
-
Spree::Calculator
- Object
- Spree::Calculator
- SolidusPromotions::Calculators::TieredFlatRate
- Includes:
- PromotionCalculator
- Defined in:
- app/models/solidus_promotions/calculators/tiered_flat_rate.rb
Overview
A calculator that applies tiered flat-rate discounts based on discountable amount thresholds.
This calculator allows defining multiple discount tiers where each tier specifies a minimum discountable amount threshold and the corresponding discount amount to apply. The calculator selects the highest tier that the item qualifies for based on its discountable amount.
If the item doesn’t meet any tier threshold, the base amount is used. The discount is only applied if the currency matches the preferred currency.
Instance Method Summary collapse
-
#compute_item(object) ⇒ BigDecimal
(also: #compute_shipment, #compute_line_item)
Computes the tiered flat-rate discount for an item.
Methods included from PromotionCalculator
Instance Method Details
#compute_item(object) ⇒ BigDecimal Also known as: compute_shipment, compute_line_item
Computes the tiered flat-rate discount for an item.
Evaluates the item’s discountable amount against all defined tiers and selects the highest tier threshold that the item meets or exceeds. Returns the discount amount associated with that tier, or the base amount if no tier threshold is met. Returns 0 if the currency doesn’t match.
83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/solidus_promotions/calculators/tiered_flat_rate.rb', line 83 def compute_item(object) _base, amount = preferred_tiers.sort.reverse.detect do |value, _| object.discountable_amount >= value end if preferred_currency.casecmp(object.currency).zero? amount || preferred_base_amount else Spree::ZERO end end |