Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Per item calculator should only apply to matching products

Fixes #1524

Merges #1526
  • Loading branch information...
commit 521f0268e8bc4574a2e126c62e2554390fd58967 1 parent 8e263b6
Andrea Schiavini authored radar committed
View
8 core/app/models/spree/calculator/per_item.rb
@@ -11,8 +11,14 @@ def self.description
def compute(object=nil)
return 0 if object.nil?
self.preferred_amount * object.line_items.reduce(0) do |sum, value|
- sum + value.quantity
+ value_to_add = (target_products().include?(value.product) ? value.quantity : 0)
+ sum + value_to_add
end
end
+
+ def target_products
+ #TODO: product groups?
+ self.calculable.promotion.rules.map(&:products).flatten
+ end
end
end
View
8 core/spec/models/calculator/per_item_spec.rb
@@ -2,16 +2,20 @@
describe Spree::Calculator::PerItem do
# Like an order object, but not quite...
- let!(:line_items) { [double("LineItem", :quantity => 5)] * 3 }
+ let!(:line_items) { [double("LineItem", :quantity => 5, :product => :a_product )] * 3 }
let!(:object) { double("Order", :line_items => line_items) }
- let!(:calculator) { Spree::Calculator::PerItem.new(:preferred_amount => 10) }
+ let!(:mock_product_set) { double("Array") }
+ let!(:calculator) { Spree::Calculator::PerItem.new(:preferred_amount => 10) }
# regression test for #1414
it "correctly calculates per item shipping" do
+ mock_product_set.stub(:include?).with(any_args()).and_return(true)
+ calculator.stub(:target_products).and_return(mock_product_set)
calculator.compute(object).to_f.should == 150 # 5 x 3 x 10
end
it "returns 0 when no object passed" do
+ calculator.stub(:target_products).and_return(mock_product_set)
calculator.compute.should == 0
end
Please sign in to comment.
Something went wrong with that request. Please try again.