Sha256: 8f7c65a112e4239b393f47adaaf9d557a6b1adbb426f4927e2e970df162b3609

Contents?: true

Size: 1.99 KB

Versions: 72

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

72 entries across 72 versions & 1 rubygems

Version Path
spree_core-3.2.9 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.14 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.6 app/models/spree/promotion/rules/item_total.rb
spree_core-3.2.8 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.13 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.5 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.4 app/models/spree/promotion/rules/item_total.rb
spree_core-3.2.7 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.12 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.3 app/models/spree/promotion/rules/item_total.rb
spree_core-3.2.6 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.11 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.10 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.2 app/models/spree/promotion/rules/item_total.rb
spree_core-3.2.5 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.9 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.1 app/models/spree/promotion/rules/item_total.rb
spree_core-3.2.4 app/models/spree/promotion/rules/item_total.rb
spree_core-3.1.8 app/models/spree/promotion/rules/item_total.rb
spree_core-3.3.0 app/models/spree/promotion/rules/item_total.rb