Class: SolidusFriendlyPromotions::FriendlyPromotionAdjuster::PersistDiscountedOrder

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_friendly_promotions/friendly_promotion_adjuster/persist_discounted_order.rb

Instance Method Summary collapse

Constructor Details

#initialize(order) ⇒ PersistDiscountedOrder

Returns a new instance of PersistDiscountedOrder.



6
7
8
# File 'app/models/solidus_friendly_promotions/friendly_promotion_adjuster/persist_discounted_order.rb', line 6

def initialize(order)
  @order = order
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/solidus_friendly_promotions/friendly_promotion_adjuster/persist_discounted_order.rb', line 10

def call
  order.line_items.each do |line_item|
    update_adjustments(line_item, line_item.current_discounts)
  end

  order.shipments.each do |shipment|
    update_adjustments(shipment, shipment.current_discounts)
  end

  order.shipments.flat_map(&:shipping_rates).each do |shipping_rate|
    shipping_rate.discounts = shipping_rate.current_discounts.map do |discount|
      SolidusFriendlyPromotions::ShippingRateDiscount.create!(
        shipping_rate: shipping_rate,
        amount: discount.amount,
        label: discount.label,
        benefit: discount.source
      )
    end
  end
  order.reset_current_discounts
  order
end