Permalink
Browse files

Re-add matching products limiter to per_item calculator

As per #1526, related to #1524 and #1596.
  • Loading branch information...
1 parent 82a45fc commit e2395da7c7fa600c145d545f3c1c85ca0b004207 @radar radar committed May 25, 2012
Showing with 17 additions and 1 deletion.
  1. +17 −1 core/app/models/spree/calculator/per_item.rb
@@ -11,7 +11,23 @@ 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 = matching_products.include?(value.product) ? value.quantity : 0
+ sum + value_to_add
+ end
+ end
+
+ # Returns all products that match this calculator, but only if the calculator
+ # is attached to a promotion. If attached to a ShippingMethod, nil is returned.
+ def matching_products
+ # Regression check for #1596
+ # Calculator::PerItem can be used in two cases.
+ # The first is in a typical promotion, providing a discount per item of a particular item
+ # The second is a ShippingMethod, where it applies to an entire order
+ #
+ # Shipping methods do not have promotions attached, but promotions do
+ # Therefore we must check for promotions
+ if self.calculable.respond_to?(:promotion)
+ self.calculable.promotion.rules.map(&:products).flatten
end
end
end

0 comments on commit e2395da

Please sign in to comment.