7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'app/models/spree/calculator/related_product_discount.rb', line 7
def compute(object)
if object.is_a?(Array)
return if object.empty?
order = object.first.order
else
order = object
end
return unless eligible?(order)
total = order.line_items.inject(0) do |sum, line_item|
relations = Spree::Relation.where(*discount_query(line_item))
discount_applies_to = relations.map { |rel| rel.related_to.master }
order.line_items.each do |li|
next li unless discount_applies_to.include? li.variant
discount = relations.detect { |rel| rel.related_to.master == li.variant }.discount_amount
sum += if li.quantity < line_item.quantity
(discount * li.quantity)
else
(discount * line_item.quantity)
end
end
sum
end
total
end
|