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-3.2.3 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.1.8 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.0.8 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.2.2 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.2.1 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.2.0 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.2.0.alpha app/models/spree/promotion_handler/shipping.rb
solidus_core-3.0.7 app/models/spree/promotion_handler/shipping.rb
solidus_core-2.11.17 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.1.7 app/models/spree/promotion_handler/shipping.rb
solidus_core-2.11.16 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.0.6 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.1.6 app/models/spree/promotion_handler/shipping.rb
solidus_core-2.11.15 app/models/spree/promotion_handler/shipping.rb
solidus_core-2.11.14 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.0.5 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.1.5 app/models/spree/promotion_handler/shipping.rb
solidus_core-2.11.13 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.0.4 app/models/spree/promotion_handler/shipping.rb
solidus_core-3.1.4 app/models/spree/promotion_handler/shipping.rb