Sha256: 7534820a16d33d5fe1e04824f4d97a4d53b0e23beec9450eea2f844600bfc348

Contents?: true

Size: 1.62 KB

Versions: 33

Compression:

Stored size: 1.62 KB

Contents

module Spree
  module PromotionHandler
    # Decides which promotion should be activated given the current order context
    #
    # By activated it doesn't necessarily mean that the order will have a
    # discount for every activated promotion. It means that the discount will be
    # created and might eventually become eligible. The intention here is to
    # reduce overhead. e.g. a promotion that requires item A to be eligible
    # shouldn't be eligible unless item A is added to the order.
    #
    # It can be used as a wrapper for custom handlers as well. Different
    # applications might have completely different requirements to make
    # the promotions system accurate and performant. Here they can plug custom
    # handler to activate promos as they wish once an item is added to cart
    class Cart
      attr_reader :line_item, :order, :store
      attr_accessor :error, :success

      def initialize(order, line_item = nil)
        @order = order
        @line_item = line_item
        @store = order.store
      end

      def activate
        promotions.each do |promotion|
          if (line_item && promotion.eligible?(line_item)) || promotion.eligible?(order)
            promotion.activate(line_item: line_item, order: order)
          else
            promotion.deactivate(line_item: line_item, order: order)
          end
        end
      end

      private

      def promotions
        promotion_scope.find_by_sql("#{order.promotions.active.to_sql} UNION #{promotion_scope.active.where(code: nil, path: nil).to_sql}")
      end

      def promotion_scope
        ::Spree::Promotion.for_store(store)
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
spree_core-4.10.1 app/models/spree/promotion_handler/cart.rb
spree_core-4.10.0 app/models/spree/promotion_handler/cart.rb
spree_core-4.9.0 app/models/spree/promotion_handler/cart.rb
spree_core-4.8.3 app/models/spree/promotion_handler/cart.rb
spree_core-4.8.2 app/models/spree/promotion_handler/cart.rb
spree_core-4.7.3 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.6 app/models/spree/promotion_handler/cart.rb
spree_core-4.5.5 app/models/spree/promotion_handler/cart.rb
spree_core-4.3.3 app/models/spree/promotion_handler/cart.rb
spree_core-4.4.1 app/models/spree/promotion_handler/cart.rb
spree_core-4.5.4 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.5 app/models/spree/promotion_handler/cart.rb
spree_core-4.7.2 app/models/spree/promotion_handler/cart.rb
spree_core-4.7.1 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.4 app/models/spree/promotion_handler/cart.rb
spree_core-4.7.0 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.3 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.2 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.1 app/models/spree/promotion_handler/cart.rb
spree_core-4.6.0 app/models/spree/promotion_handler/cart.rb