Class: SolidusFriendlyPromotions::Conditions::MinimumQuantity

Inherits:
SolidusFriendlyPromotions::Condition show all
Includes:
OrderLevelCondition
Defined in:
app/models/solidus_friendly_promotions/conditions/minimum_quantity.rb

Overview

Promotion condition for ensuring an order contains a minimum quantity of applicable items.

This promotion condition is only compatible with the “all” match policy. It doesn’t make a lot of sense to use it without that policy as it reduces it to a simple quantity check across the entire order which would be better served by an item total condition.

Instance Method Summary collapse

Methods included from OrderLevelCondition

#applicable?, #level

Methods inherited from SolidusFriendlyPromotions::Condition

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

Instance Method Details

#eligible?(order) ⇒ Boolean

Will look at all of the “applicable” line items in the order and determine if the sum of their quantity is greater than the minimum.

“Applicable” items are ones that pass all eligibility checks of applicable conditions.

When false is returned, the reason will be included in the ‘eligibility_errors` object.

Parameters:

  • order (Spree::Order)

    the order we want to check eligibility on

Returns:

  • (Boolean)

    true if promotion is eligible, false otherwise



29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/solidus_friendly_promotions/conditions/minimum_quantity.rb', line 29

def eligible?(order)
  if benefit.applicable_line_items(order).sum(&:quantity) < preferred_minimum_quantity
    eligibility_errors.add(
      :base,
      eligibility_error_message(:quantity_less_than_minimum, count: preferred_minimum_quantity),
      error_code: :quantity_less_than_minimum
    )
  end

  eligibility_errors.empty?
end