forked from spree/spree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weight_scale.rb
28 lines (24 loc) · 901 Bytes
/
weight_scale.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Spree
class Calculator::WeightScale < Calculator
preference :rule, :string
preference :default_weight, :decimal, :default => 1
preference :default_price, :decimal, :default => 0
def self.description
I18n.t(:weight_scale)
end
# as object we always get line items, as calculable we have Coupon, ShippingMethod
def compute(object)
line_items = object.is_a?(Order) ? object.line_items : object
if self.preferred_rule =~ /^(\d*\:\d*\;)*$|^(\d*\:\d*\;)*(\d*\:\d*)$/
total_weight = line_items.sum do |li|
(li.variant.weight || self.preferred_default_weight) * li.quantity
end
self.preferred_rule.split(";").each do |rule|
weight, price = rule.split(":")
return price.to_f if total_weight.to_f < weight.to_f
end
end
return self.preferred_default_price.to_f
end
end
end