Class: SolidusPromotions::Conditions::LineItemProduct

Inherits:
SolidusPromotions::Condition show all
Includes:
LineItemLevelCondition, ProductCondition
Defined in:
app/models/solidus_promotions/conditions/line_item_product.rb

Overview

A condition to apply a promotion only to line items with or without a chosen product

Constant Summary collapse

MATCH_POLICIES =
%w[include exclude].freeze

Instance Method Summary collapse

Methods included from ProductCondition

included, #preload_relations, #product_ids_string, #product_ids_string=

Methods included from LineItemLevelCondition

included, #level

Methods inherited from SolidusPromotions::Condition

#applicable?, applicable_to, #eligibility_errors, #eligible?, eligible_method_for, inherited, #level, #preload_relations, #to_partial_path, #updateable?

Instance Method Details

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

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/solidus_promotions/conditions/line_item_product.rb', line 16

def line_item_eligible?(line_item, _options = {})
  order_includes_product = product_ids.include?(line_item.variant.product_id)
  success = inverse? ? !order_includes_product : order_includes_product

  unless success
    message_code = inverse? ? :has_excluded_product : :no_applicable_products
    eligibility_errors.add(
      :base,
      eligibility_error_message(message_code),
      error_code: message_code
    )
  end

  success
end