Class: SolidusPromotions::Calculators::TieredPercentOnEligibleItemQuantity
- Inherits:
-
Spree::Calculator
- Object
- Spree::Calculator
- SolidusPromotions::Calculators::TieredPercentOnEligibleItemQuantity
- Includes:
- PromotionCalculator
- Defined in:
- app/models/solidus_promotions/calculators/tiered_percent_on_eligible_item_quantity.rb
Overview
A calculator that applies tiered percentage discounts based on the total quantity of eligible items.
This calculator defines discount tiers based on the combined quantity of all eligible line items in an order (not their monetary value). Each tier specifies a minimum quantity threshold and the corresponding percentage discount to apply. The calculator selects the highest tier that the order’s eligible item quantity meets or exceeds.
The tier thresholds are evaluated against the total quantity of eligible line items, but the percentage discount is applied to each individual item’s discountable amount. This makes it ideal for “buy more, save more” promotions based on item count rather than order value.
If the total eligible quantity doesn’t meet any tier threshold, the base percentage is used. The discount is only applied if the currency matches the preferred currency.
Instance Method Summary collapse
-
#compute_item(item) ⇒ BigDecimal
(also: #compute_shipment, #compute_shipping_rate, #compute_line_item)
Computes the tiered percentage discount for an item based on total eligible item quantity.
Methods included from PromotionCalculator
Instance Method Details
#compute_item(item) ⇒ BigDecimal Also known as: compute_shipment, compute_shipping_rate, compute_line_item
Computes the tiered percentage discount for an item based on total eligible item quantity.
Evaluates the total quantity of all eligible line items in the order against all defined tiers and selects the highest tier threshold that is met or exceeded. Returns a percentage of the item’s discountable amount based on the matching tier, or the base percentage if no tier threshold is met. Returns 0 if the currency doesn’t match.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/models/solidus_promotions/calculators/tiered_percent_on_eligible_item_quantity.rb', line 89 def compute_item(item) order = item.order _base, percent = preferred_tiers.sort.reverse.detect do |value, _| eligible_line_items_quantity_total(order) >= value end if preferred_currency.casecmp(order.currency).zero? round_to_currency(item.discountable_amount * (percent || preferred_base_percent) / 100, preferred_currency) else Spree::ZERO end end |