Skip to content
This repository
Browse code

Re-add matching products limiter to per_item calculator

As per #1526, related to #1524 and #1596.
  • Loading branch information...
commit e2395da7c7fa600c145d545f3c1c85ca0b004207 1 parent 82a45fc
Ryan Bigg authored May 25, 2012
18  core/app/models/spree/calculator/per_item.rb
@@ -11,7 +11,23 @@ def self.description
11 11
     def compute(object=nil)
12 12
       return 0 if object.nil?
13 13
       self.preferred_amount * object.line_items.reduce(0) do |sum, value|
14  
-        sum + value.quantity
  14
+        value_to_add = matching_products.include?(value.product) ? value.quantity : 0
  15
+        sum + value_to_add
  16
+      end
  17
+    end
  18
+
  19
+    # Returns all products that match this calculator, but only if the calculator
  20
+    # is attached to a promotion. If attached to a ShippingMethod, nil is returned.
  21
+    def matching_products
  22
+      # Regression check for #1596
  23
+      # Calculator::PerItem can be used in two cases.
  24
+      # The first is in a typical promotion, providing a discount per item of a particular item
  25
+      # The second is a ShippingMethod, where it applies to an entire order
  26
+      #
  27
+      # Shipping methods do not have promotions attached, but promotions do
  28
+      # Therefore we must check for promotions
  29
+      if self.calculable.respond_to?(:promotion)
  30
+        self.calculable.promotion.rules.map(&:products).flatten
15 31
       end
16 32
     end
17 33
   end

0 notes on commit e2395da

Please sign in to comment.
Something went wrong with that request. Please try again.