Sha256: 25a4b7230acd0bfa2573eaa6a0e5c0411522940c92a6426c4224b033612d75b8

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 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
        connected_promotions.each do |order_promotion|
          order_promotion.promotion.activate(
            order: order,
            promotion_code: order_promotion.promotion_code,
          )
        end

        not_connected_automatic_promotions.each do |promotion|
          promotion.activate(order: order)
        end
      end

      private

      def not_connected_automatic_promotions
        automatic_promotions - connected_promotions.map(&:promotion)
      end

      def automatic_promotions
        @automatic_promotions ||= active_free_shipping_promotions.
          where(apply_automatically: true).
          to_a.
          uniq
      end

      def connected_promotions
        @connected_promotions ||= order.order_promotions.
          joins(:promotion).
          includes(:promotion).
          merge(active_free_shipping_promotions).
          to_a.
          uniq
      end

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
solidus_core-2.3.1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.2.2 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.3.0 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.3.0.rc3 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.3.0.rc2 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.3.0.rc1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.3.0.beta1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.2.1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.2.0 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.2.0.rc1 app/models/spree/promotion_handler/free_shipping.rb
solidus_core-2.2.0.beta1 app/models/spree/promotion_handler/free_shipping.rb