Class: Spree::Promotion::Actions::FreeShipping

Inherits:
Spree::PromotionAction show all
Defined in:
app/models/spree/promotion/actions/free_shipping.rb

Instance Method Summary collapse

Methods inherited from Spree::PromotionAction

#to_partial_path

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#compute_amount(shipment) ⇒ Object


32
33
34
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 32

def compute_amount(shipment)
  shipment.cost * -1
end

#labelObject


28
29
30
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 28

def label
  "#{I18n.t('spree.promotion')} (#{promotion.name})"
end

#perform(payload = {}) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 7

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

  created_adjustments = order.shipments.map do |shipment|
    next if promotion_credit_exists?(shipment)

    shipment.adjustments.create!(
      order: shipment.order,
      amount: compute_amount(shipment),
      source: self,
      promotion_code: promotion_code,
      label: label
    )
  end

  # Did we actually end up creating any adjustments?
  # If so, then this action should be classed as 'successful'
  created_adjustments.any?
end

#remove_from(order) ⇒ void

This method returns an undefined value.

Removes any adjustments generated by this action from the order's

shipments.

Parameters:

  • order (Spree::Order)

    the order to remove the action from.


40
41
42
43
44
45
46
47
48
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 40

def remove_from(order)
  order.shipments.each do |shipment|
    shipment.adjustments.each do |adjustment|
      if adjustment.source == self
        shipment.adjustments.destroy(adjustment)
      end
    end
  end
end