Sha256: bbeed45914c846dcee8e7ccf988486f90e5ed97ac619292bdbed2a3e054cfd3e

Contents?: true

Size: 1.63 KB

Versions: 43

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Spree
  class Promotion < Spree::Base
    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
          "#{I18n.t('spree.promotion')} (#{promotion.name})"
        end

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

        # Removes any adjustments generated by this action from the order's
        #  shipments.
        # @param order [Spree::Order] the order to remove the action from.
        # @return [void]
        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

        private

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

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
solidus_core-2.8.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.6.3 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.8.0 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.7.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.6.2 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.10 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.9 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.8 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.7 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.6 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.5 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.4 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.3 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core_devise_token_auth-2.8.0.alpha.0 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.7.0 app/models/spree/promotion/actions/free_shipping.rb