Class: Spree::Promotion::Actions::CreateItemAdjustments

Inherits:
Spree::PromotionAction show all
Includes:
AdjustmentSource, CalculatedAdjustments
Defined in:
app/models/spree/promotion/actions/create_item_adjustments.rb

Direct Known Subclasses

CreateQuantityAdjustments

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Spree::Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Method Details

#compute_amount(adjustable) ⇒ Object

Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total



31
32
33
34
35
36
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 31

def compute_amount(adjustable)
  order = adjustable.is_a?(Order) ? adjustable : adjustable.order
  return 0 unless promotion.line_item_actionable?(order, adjustable)
  promotion_amount = self.calculator.compute(adjustable).to_f.abs
  [adjustable.amount, promotion_amount].min * -1
end

#perform(payload = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/models/spree/promotion/actions/create_item_adjustments.rb', line 15

def perform(payload = {})
  order = payload[:order]
  promotion = payload[:promotion]
  promotion_code = payload[:promotion_code]

  result = false

  line_items_to_adjust(promotion, order).each do |line_item|
    current_result = create_adjustment(line_item, order, promotion_code)
    result ||= current_result
  end
  return result
end