Class: SolidusPromotions::OrderAdjuster

Inherits:
Object
  • Object
show all
Defined in:
app/models/solidus_promotions/order_adjuster.rb,
app/models/solidus_promotions/order_adjuster/discount_order.rb,
app/models/solidus_promotions/order_adjuster/load_promotions.rb,
app/models/solidus_promotions/order_adjuster/choose_discounts.rb,
app/models/solidus_promotions/order_adjuster/persist_discounted_order.rb

Defined Under Namespace

Classes: ChooseDiscounts, DiscountOrder, LoadPromotions, PersistDiscountedOrder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order, dry_run_promotion: nil) ⇒ OrderAdjuster

Returns a new instance of OrderAdjuster.



7
8
9
10
11
# File 'app/models/solidus_promotions/order_adjuster.rb', line 7

def initialize(order, dry_run_promotion: nil)
  @order = order
  @dry_run = !!dry_run_promotion
  @promotions = LoadPromotions.new(order: order, dry_run_promotion: dry_run_promotion).call
end

Instance Attribute Details

#dry_runObject (readonly)

Returns the value of attribute dry_run.



5
6
7
# File 'app/models/solidus_promotions/order_adjuster.rb', line 5

def dry_run
  @dry_run
end

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'app/models/solidus_promotions/order_adjuster.rb', line 5

def order
  @order
end

#promotionsObject (readonly)

Returns the value of attribute promotions.



5
6
7
# File 'app/models/solidus_promotions/order_adjuster.rb', line 5

def promotions
  @promotions
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/solidus_promotions/order_adjuster.rb', line 13

def call
  order.reset_current_discounts

  return order unless SolidusPromotions::Promotion.order_activatable?(order)

  discounted_order = DiscountOrder.new(order, promotions, dry_run: dry_run).call

  PersistDiscountedOrder.new(discounted_order).call unless dry_run

  order.reset_current_discounts

  unless dry_run
    # Since automations might have added a line item, we need to recalculate item total and item count here.
    order.item_total = order.line_items.sum(&:amount)
    order.item_count = order.line_items.sum(&:quantity)
    order.promo_total = (order.line_items + order.shipments).sum(&:promo_total)
  end
  order
end