Sha256: c906c0b86e98a386f1b979b2a51726ff70792e1e31baf1414f15300324d1aff3

Contents?: true

Size: 1.58 KB

Versions: 3

Compression:

Stored size: 1.58 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)
        if promotion.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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.0.0 app/models/solidus_friendly_promotions/rules/minimum_quantity.rb
solidus_friendly_promotions-1.0.0.rc.3 app/models/solidus_friendly_promotions/rules/minimum_quantity.rb
solidus_friendly_promotions-1.0.0.rc.2 app/models/solidus_friendly_promotions/rules/minimum_quantity.rb