Sha256: 29f5724be6751f26bcb8e0f4392bc9076bfee1e1c74fe87db1800fd9c8905e0c

Contents?: true

Size: 1.61 KB

Versions: 49

Compression:

Stored size: 1.61 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]

          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

        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

49 entries across 49 versions & 1 rubygems

Version Path
solidus_core-3.2.9 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.8 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.7 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.6 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.1.9 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.5 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.4 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.3 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.1.8 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.0.8 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.2 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.1 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.0 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.2.0.alpha app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.0.7 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.11.17 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.1.7 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-2.11.16 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.0.6 app/models/spree/promotion/actions/free_shipping.rb
solidus_core-3.1.6 app/models/spree/promotion/actions/free_shipping.rb