Class: Spree::Calculator::PercentOnLineItem

Inherits:
Spree::Calculator show all
Defined in:
app/models/spree/calculator/percent_on_line_item.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::Calculator

#available?, calculators, #description, #to_s

Class Method Details

.descriptionObject



7
8
9
# File 'app/models/spree/calculator/percent_on_line_item.rb', line 7

def self.description
  Spree.t(:percent_per_item)
end

Instance Method Details

#compute(object) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/spree/calculator/percent_on_line_item.rb', line 11

def compute(object)
  return 0 if preferred_apply_only_on_full_priced_items && object.variant.compare_at_amount_in(object.currency).present?

  computed_amount = (object.amount * preferred_percent / 100).round(2)

  # We don't want to cause the promotion adjustments to push the order into a negative total.
  if computed_amount > object.amount
    object.amount
  else
    computed_amount
  end
end