Sha256: c628fad08c0b922cfac257789626317eb91bfb57afb913b9f39e9f0c7821ce1f

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

module Spree
  module PromotionHandler
    # Used for activating promotions with shipping rules
    class FreeShipping
      attr_reader :order
      attr_accessor :error, :success

      def initialize(order)
        @order = order
      end

      def activate
        promotions.each do |promotion|
          if order_promotion = existing_order_promotion(promotion)
            promotion.activate(
              order: order,
              promotion_code: order_promotion.promotion_code,
            )
          elsif promotion.apply_automatically?
            promotion.activate(order: order)
          end
        end
      end

      private

      def promotions
        Spree::Promotion.
          active.
          joins(:promotion_actions).
          merge(
            Spree::PromotionAction.of_type(
              Spree::Promotion::Actions::FreeShipping
            )
          ).
          distinct
      end

      def existing_order_promotion(promotion)
        @lookup ||= order.order_promotions.map do |order_promotion|
          [order_promotion.promotion_id, order_promotion]
        end.to_h

        @lookup[promotion.id]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_core-2.1.1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.1.0 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.1.0.rc1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.1.0.beta1 app/models/spree/promotion_handler/free_shipping.rb