Sha256: 7be3819ea8653c9763e2f9f4541f44ea16bf4d5ca6736a677daa1e4228bb6f24

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 KB

Contents

# A rule to limit a promotion based on products in the order.
# Can require all or any of the products to be present.
# Valid products either come from assigned product group or are assingned directly to the rule.
module Spree
  class Promotion
    module Rules
      class Product < PromotionRule
        has_and_belongs_to_many :products, class_name: '::Spree::Product', join_table: 'spree_products_promotion_rules', foreign_key: 'promotion_rule_id'

        MATCH_POLICIES = %w(any all none)
        preference :match_policy, :string, default: MATCH_POLICIES.first

        # scope/association that is used to test eligibility
        def eligible_products
          products
        end

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          return true if eligible_products.empty?
          if preferred_match_policy == 'all'
            eligible_products.all? {|p| order.products.include?(p) }
          elsif preferred_match_policy == 'any'
            order.products.any? {|p| eligible_products.include?(p) }
          else
            order.products.none? {|p| eligible_products.include?(p) }
          end
        end

        def actionable?(line_item)
          case preferred_match_policy
          when 'any', 'all'
            product_ids.include? line_item.variant.product_id
          when 'none'
            product_ids.exclude? line_item.variant.product_id
          else
            raise "unexpected match policy: #{preferred_match_policy.inspect}"
          end
        end

        def product_ids_string
          product_ids.join(',')
        end

        def product_ids_string=(s)
          self.product_ids = s.to_s.split(',').map(&:strip)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spree_core-2.3.13 app/models/spree/promotion/rules/product.rb
spree_core-2.3.12 app/models/spree/promotion/rules/product.rb
spree_core-2.3.11 app/models/spree/promotion/rules/product.rb
spree_core-2.3.10 app/models/spree/promotion/rules/product.rb
spree_core-2.3.9 app/models/spree/promotion/rules/product.rb
spree_core-2.3.8 app/models/spree/promotion/rules/product.rb
spree_core-2.3.7 app/models/spree/promotion/rules/product.rb
spree_core-2.3.6 app/models/spree/promotion/rules/product.rb