Class: Spree::Promotion::OrderAdjustmentsRecalculator

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/promotion/order_adjustments_recalculator.rb

Overview

This class iterates over all existing promotion adjustments and recalculates their amount and eligibility using their adjustment source.

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ OrderAdjustmentsRecalculator

Returns a new instance of OrderAdjustmentsRecalculator.



13
14
15
# File 'app/models/spree/promotion/order_adjustments_recalculator.rb', line 13

def initialize(order)
  @order = order
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/spree/promotion/order_adjustments_recalculator.rb', line 17

def call
  all_items = line_items + shipments
  all_items.each do |item|
    promotion_adjustments = item.adjustments.select(&:promotion?)

    promotion_adjustments.each { |adjustment| recalculate(adjustment) }
    Spree::Config.promotion_chooser_class.new(promotion_adjustments).update

    item.promo_total = promotion_adjustments.select(&:eligible?).sum(&:amount)
  end
  # Update and select the best promotion adjustment for the order.
  # We don't update the order.promo_total yet. Order totals are updated later
  # in #update_adjustment_total since they include the totals from the order's
  # line items and/or shipments.
  order_promotion_adjustments = order.adjustments.select(&:promotion?)
  order_promotion_adjustments.each { |adjustment| recalculate(adjustment) }
  Spree::Config.promotion_chooser_class.new(order_promotion_adjustments).update

  order.promo_total = all_items.sum(&:promo_total) +
                      order_promotion_adjustments.
                        select(&:eligible?).
                        select(&:promotion?).
                        sum(&:amount)
  order
end