Sha256: 5700a411fcff2956f607e036edc872f4843c7da23f2a28a35d02f4cc69ebb164

Contents?: true

Size: 1.1 KB

Versions: 11

Compression:

Stored size: 1.1 KB

Contents

module Spree
  class Promotion
    module Actions
      class FreeShipping < Spree::PromotionAction
        def perform(payload = {})
          order = payload[:order]
          promotion_code = payload[:promotion_code]

          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,
              promotion_code: promotion_code,
              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

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

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

        private

        def promotion_credit_exists?(shipment)
          shipment.adjustments.where(source: self).exists?
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
solidus_core-2.0.3 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-1.4.2 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.0.2 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-1.4.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.0.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.0.0 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-1.4.0 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.0.0.rc1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-1.4.0.rc1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.0.0.beta1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-1.4.0.beta1 app/models/spree/promotion/actions/free_shipping.rb