Sha256: e2ec436c56d8b94fce0137ac4af9c7efd477a9e67c15be999472cf270aa9fded

Contents?: true

Size: 1.48 KB

Versions: 130

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

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

      def initialize(order)
        @order = order
      end

      def activate
        connected_promotions.each do |order_promotion|
          if order_promotion.promotion.eligible?(order)
            order_promotion.promotion.activate(
              order: order,
              promotion_code: order_promotion.promotion_code,
            )
          end
        end

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

      private

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

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

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

      def active_shipping_promotions
        Spree::Promotion.all.
          active.
          joins(:promotion_actions).
          merge(Spree::PromotionAction.shipping)
      end
    end
  end
end

Version data entries

130 entries across 130 versions & 2 rubygems

Version Path
solidus_core-4.3.4 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.2.4 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.1.5 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.3.3 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.3.2 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.1.4 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.3.1 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.3.0 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.2.3 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.1.3 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.0.4 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.4.6 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.0.3 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.1.2 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.2.2 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.4.5 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.2.1 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.2.0 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.1.1 app/models/spree/promotion_handler/shipping.rb
solidus_core-4.0.2 app/models/spree/promotion_handler/shipping.rb