Class: Spree::Promotion::Rules::ItemTotal

Inherits:
Spree::PromotionRule show all
Defined in:
app/models/spree/promotion/rules/item_total.rb

Overview

A rule to apply to an order greater than (or greater than or equal to) a specific amount

To add extra operators please override ‘self.operators_map` or any other helper method. To customize the error message you can also override `ineligible_message`.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::PromotionRule

#actionable?, #eligibility_errors, #preload_relations, #to_partial_path

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Class Method Details

.operator_optionsObject



24
25
26
27
28
# File 'app/models/spree/promotion/rules/item_total.rb', line 24

def self.operator_options
  operators_map.map do |name, _method|
    [I18n.t(name, scope: 'spree.item_total_rule.operators'), name]
  end
end

.operators_mapObject

The list of allowed operators names mapped to their symbols.



17
18
19
20
21
22
# File 'app/models/spree/promotion/rules/item_total.rb', line 17

def self.operators_map
  {
    gte: :>=,
    gt: :>,
  }
end

Instance Method Details

#applicable?(promotable) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/spree/promotion/rules/item_total.rb', line 30

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

#eligible?(order, _options = {}) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'app/models/spree/promotion/rules/item_total.rb', line 34

def eligible?(order, _options = {})
  return false unless order.currency == preferred_currency

  unless total_for_order(order).send(operator, threshold)
    eligibility_errors.add(:base, ineligible_message, error_code: ineligible_error_code)
  end

  eligibility_errors.empty?
end