Skip to content

Commit

Permalink
Re-add matching products limiter to per_item calculator
Browse files Browse the repository at this point in the history
As per #1526, related to #1524 and #1596.
  • Loading branch information
radar committed May 25, 2012
1 parent c82264f commit 16ff1b7
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion core/app/models/spree/calculator/per_item.rb
Expand Up @@ -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
Expand Down

1 comment on commit 16ff1b7

@BDQ
Copy link
Member

@BDQ BDQ commented on 16ff1b7 May 25, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@radar - should this calculator not be split into two? We can register one for shipping and another for promo

Please sign in to comment.