Module: SolidusLegacyPromotions::SpreeInMemoryOrderUpdaterPatch

Defined in:
app/patches/models/solidus_legacy_promotions/spree_in_memory_order_updater_patch.rb

Instance Method Summary collapse

Instance Method Details

#assign_item_totalsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/patches/models/solidus_legacy_promotions/spree_in_memory_order_updater_patch.rb', line 19

def assign_item_totals
  [*line_items, *shipments].each do |item|
    Spree::Config.item_total_class.new(item).recalculate!

    # The cancellation_total isn't persisted anywhere but is included in
    # the adjustment_total.
    #
    # Core doesn't have "eligible" adjustments anymore, so we need to
    # override the adjustment_total calculation to exclude them for legacy
    # promotions.
    item.adjustment_total = item.adjustments
      .select(&:eligible?)
      .reject(&:included?)
      .reject(&:marked_for_destruction?)
      .sum(&:amount)
  end
end

#update_adjustment_total(persist:) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/patches/models/solidus_legacy_promotions/spree_in_memory_order_updater_patch.rb', line 5

def update_adjustment_total(persist:)
  update_adjustments(persist:)

  all_items = (line_items + shipments).reject(&:marked_for_destruction?)
  valid_adjustments = adjustments.select(&:eligible?).reject(&:marked_for_destruction?)
  order_tax_adjustments = valid_adjustments.select(&:tax?)

  order.adjustment_total = all_items.sum(&:adjustment_total) + valid_adjustments.sum(&:amount)
  order.included_tax_total = all_items.sum(&:included_tax_total) + order_tax_adjustments.select(&:included?).sum(&:amount)
  order.additional_tax_total = all_items.sum(&:additional_tax_total) + order_tax_adjustments.reject(&:included?).sum(&:amount)

  recalculate_order_total
end