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 Base

page

Methods included from Spree::Preferences::Preferable

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

Instance Method Details

#compute_amount(shipment) ⇒ Object


26
27
28
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 26

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

#labelObject


22
23
24
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 22

def label
  "#{Spree.t(:promotion)} (#{promotion.name})"
end

#perform(payload = {}) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/spree/promotion/actions/free_shipping.rb', line 5

def perform(payload={})
  order = payload[:order]
  results = order.shipments.map do |shipment|
    return false if promotion_credit_exists?(shipment)
    shipment.adjustments.create!(
      order: shipment.order, 
      amount: compute_amount(shipment),
      source: self,
      label: label,
    )
    true
  end
  # Did we actually end up applying any adjustments?
  # If so, then this action should be classed as 'successful'
  results.any? { |r| r == true }
end