Add by-weight shipping calculator

This commit is contained in:
Rohan Mitchell
2012-06-29 12:46:33 +10:00
parent 73e601315b
commit 28d2292de0
3 changed files with 32 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
module OpenFoodWeb
class Calculator::Weight < Spree::Calculator
preference :per_kg, :decimal, :default => 0.0
attr_accessible :preferred_per_kg
def self.description
"Weight (per kg)"
end
def compute(object)
total_weight = object.line_items.inject(0) { |sum, li| sum+li.variant.weight }
total_weight * self.preferred_per_kg
end
end
end

View File

@@ -27,6 +27,7 @@ module Openfoodweb
# Register Spree calculators
initializer "spree.register.calculators" do |app|
app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Itemwise
app.config.spree.calculators.shipping_methods << OpenFoodWeb::Calculator::Weight
end

View File

@@ -0,0 +1,16 @@
require 'spec_helper'
describe OpenFoodWeb::Calculator::Weight do
it "computes shipping cost for an order by total weight" do
variant_1 = double(:variant, :weight => 10)
variant_2 = double(:variant, :weight => 20)
line_item_1 = double(:line_item, :variant => variant_1)
line_item_2 = double(:line_item, :variant => variant_2)
order = double(:order, :line_items => [line_item_1, line_item_2])
subject.set_preference(:per_kg, 10)
subject.compute(order).should == 300
end
end