Skip to content

Commit

Permalink
change discount to percentage of matched product
Browse files Browse the repository at this point in the history
  • Loading branch information
imme5150 committed Mar 1, 2012
1 parent 4da6f63 commit a8a99fb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
12 changes: 5 additions & 7 deletions app/models/spree/calculator/related_product_discount.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ def compute(object)

order.line_items.each do |li|
if discount_applies_to.include? li.product
discount = relations.detect {|rel| rel.related_to == li.product}.discount_amount

total += if li.quantity < line_item.quantity
(discount * li.quantity)
else
(discount * line_item.quantity)
end
discount_percent = relations.detect {|rel| rel.related_to == li.product}.discount_amount

# only apply the discount as many times as the minimum quantity of matching products
quantity = [li.quantity, line_item.quantity].min
total += (li.price * discount_percent / 100.0) * quantity
end
end

Expand Down
1 change: 1 addition & 0 deletions app/models/spree/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class Spree::Relation < ActiveRecord::Base
belongs_to :related_to, :polymorphic => true

validates_presence_of :relation_type, :relatable, :related_to
validates_inclusion_of :discount_amount, :in => 0..100
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<tbody>
<% product.relations.each do |relation| %>
<tr id="<%= dom_id relation %>">
<td><%= relation.related_to.name %></td>
<td><%= relation.discount_amount != 0 ? number_to_currency(relation.discount_amount) : "-" %></td>
<td><%= link_to relation.related_to.name, relation.related_to %></td>
<td><%= relation.discount_amount != 0 ? number_to_percentage(relation.discount_amount, :precision => 0) : "none" %></td>
<td><%= relation.relation_type.name %></td>
<td width="70px">
<%= link_to_delete relation, {:url => admin_product_relation_url(relation.relatable, relation)} %>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
en:
new_relation_type: New Relation Type
discount_amount: Discount Amount
discount_amount: Discount %
related_product_discount: Related Product Discount
relation_types: Relation Types
applies_to: Applies To
Expand Down

0 comments on commit a8a99fb

Please sign in to comment.