Sha256: 1f5374d840cbdf602bc5860ebbd15207e6924ed2d00b8e6e9f4e6cbcff19ec91

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module SolidusFriendlyPromotions
  module Rules
    # Promotion rule for ensuring an order contains a minimum quantity of
    # applicable items.
    #
    # This promotion rule 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 rule.
    class MinimumQuantity < PromotionRule
      include OrderLevelRule

      validates :preferred_minimum_quantity, numericality: {only_integer: true, greater_than: 0}

      preference :minimum_quantity, :integer, default: 1

      # 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 rules.
      #
      # When false is returned, the reason will be included in the
      # `eligibility_errors` object.
      #
      # @param order [Spree::Order] the order we want to check eligibility on
      # @return [Boolean] true if promotion is eligible, false otherwise
      def eligible?(order)
        applicable_line_items = order.line_items.select do |line_item|
          promotion.rules.select do |rule|
            rule.applicable?(line_item)
          end.all? { _1.eligible?(line_item) }
        end

        if applicable_line_items.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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0.rc.1 app/models/solidus_friendly_promotions/rules/minimum_quantity.rb
solidus_friendly_promotions-1.0.0.pre app/models/solidus_friendly_promotions/rules/minimum_quantity.rb