Sha256: 9f298e71a42eca2dccdbec2e29a19201b84e2ded7f5b756b7aa0ce194dc6dd6a

Contents?: true

Size: 1.99 KB

Versions: 20

Compression:

Stored size: 1.99 KB

Contents

# A rule to apply to an order greater than (or greater than or equal to)
# a specific amount
module Spree
  class Promotion
    module Rules
      class ItemTotal < PromotionRule
        preference :amount_min, :decimal, default: 100.00
        preference :operator_min, :string, default: '>'
        preference :amount_max, :decimal, default: 1000.00
        preference :operator_max, :string, default: '<'

        OPERATORS_MIN = ['gt', 'gte']
        OPERATORS_MAX = ['lt', 'lte']

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, _options = {})
          item_total = order.item_total

          lower_limit_condition = item_total.send(preferred_operator_min == 'gte' ? :>= : :>, BigDecimal.new(preferred_amount_min.to_s))
          upper_limit_condition = item_total.send(preferred_operator_max == 'lte' ? :<= : :<, BigDecimal.new(preferred_amount_max.to_s))

          eligibility_errors.add(:base, ineligible_message_max) unless upper_limit_condition
          eligibility_errors.add(:base, ineligible_message_min) unless lower_limit_condition

          eligibility_errors.empty?
        end

        private

        def formatted_amount_min
          Spree::Money.new(preferred_amount_min).to_s
        end

        def formatted_amount_max
          Spree::Money.new(preferred_amount_max).to_s
        end

        def ineligible_message_max
          if preferred_operator_max == 'gte'
            eligibility_error_message(:item_total_more_than_or_equal, amount: formatted_amount_max)
          else
            eligibility_error_message(:item_total_more_than, amount: formatted_amount_max)
          end
        end

        def ineligible_message_min
          if preferred_operator_min == 'gte'
            eligibility_error_message(:item_total_less_than, amount: formatted_amount_min)
          else
            eligibility_error_message(:item_total_less_than_or_equal, amount: formatted_amount_min)
          end
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
spree_core-3.4.6 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.6 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.5 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.4 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.3 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.2 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.1 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.0 app/models/spree/promotion/rules/item_total.rb
spree_core-3.5.0 app/models/spree/promotion/rules/item_total.rb
spree_core-3.6.0.rc1 app/models/spree/promotion/rules/item_total.rb
spree_core-3.5.0.rc2 app/models/spree/promotion/rules/item_total.rb
spree_core-3.5.0.rc1 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.5 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.4 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.3 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.2 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.1 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.0 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.0.rc2 app/models/spree/promotion/rules/item_total.rb
spree_core-3.4.0.rc1 app/models/spree/promotion/rules/item_total.rb