Class: SolidusPromotions::Conditions::Product

Inherits:
SolidusPromotions::Condition show all
Includes:
LineItemApplicableOrderLevelCondition
Defined in:
app/models/solidus_promotions/conditions/product.rb

Overview

A condition 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 condition.

Constant Summary collapse

MATCH_POLICIES =
%w[any all none only].freeze

Instance Method Summary collapse

Methods included from LineItemApplicableOrderLevelCondition

#applicable?, #eligible?, included, #level

Methods inherited from SolidusPromotions::Condition

#applicable?, #eligibility_errors, #eligible?, #level, #to_partial_path, #updateable?

Instance Method Details

#eligible_productsObject

scope/association that is used to test eligibility



30
31
32
# File 'app/models/solidus_promotions/conditions/product.rb', line 30

def eligible_products
  products
end

#line_item_eligible?(line_item, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
# File 'app/models/solidus_promotions/conditions/product.rb', line 64

def line_item_eligible?(line_item, _options = {})
  # The order level eligibility check happens first, and if none of the products
  # are in the order, then no line items should be available to check.
  raise "This should not happen" if preferred_match_policy == "none"
  product_ids.include?(line_item.variant.product_id)
end

#order_eligible?(order) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/solidus_promotions/conditions/product.rb', line 34

def order_eligible?(order)
  return true if eligible_products.empty?

  case preferred_match_policy
  when "all"
    unless eligible_products.all? { |product| order_products(order).include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:missing_product), error_code: :missing_product)
    end
  when "any"
    unless order_products(order).any? { |product| eligible_products.include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:no_applicable_products),
        error_code: :no_applicable_products)
    end
  when "none"
    unless order_products(order).none? { |product| eligible_products.include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product),
        error_code: :has_excluded_product)
    end
  when "only"
    unless order_products(order).all? { |product| eligible_products.include?(product) }
      eligibility_errors.add(:base, eligibility_error_message(:has_excluded_product),
        error_code: :has_excluded_product)
    end
  else
    raise "unexpected match policy: #{preferred_match_policy.inspect}"
  end

  eligibility_errors.empty?
end

#preload_relationsObject



19
20
21
# File 'app/models/solidus_promotions/conditions/product.rb', line 19

def preload_relations
  [:products]
end

#product_ids_stringObject



71
72
73
# File 'app/models/solidus_promotions/conditions/product.rb', line 71

def product_ids_string
  product_ids.join(",")
end

#product_ids_string=(product_ids) ⇒ Object



75
76
77
# File 'app/models/solidus_promotions/conditions/product.rb', line 75

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