From a8a99fbcd859e51ac9d0e721ee39ec338cfd4357 Mon Sep 17 00:00:00 2001 From: Josh Shupack Date: Wed, 29 Feb 2012 17:34:59 -0800 Subject: [PATCH] change discount to percentage of matched product --- .../spree/calculator/related_product_discount.rb | 12 +++++------- app/models/spree/relation.rb | 1 + .../admin/products/_related_products_table.html.erb | 4 ++-- config/locales/en.yml | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/models/spree/calculator/related_product_discount.rb b/app/models/spree/calculator/related_product_discount.rb index 9aa98be7..22db324b 100644 --- a/app/models/spree/calculator/related_product_discount.rb +++ b/app/models/spree/calculator/related_product_discount.rb @@ -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 diff --git a/app/models/spree/relation.rb b/app/models/spree/relation.rb index b90bbf49..ebe73fea 100644 --- a/app/models/spree/relation.rb +++ b/app/models/spree/relation.rb @@ -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 diff --git a/app/views/spree/admin/products/_related_products_table.html.erb b/app/views/spree/admin/products/_related_products_table.html.erb index 747483ed..1e3374c9 100644 --- a/app/views/spree/admin/products/_related_products_table.html.erb +++ b/app/views/spree/admin/products/_related_products_table.html.erb @@ -10,8 +10,8 @@ <% product.relations.each do |relation| %> - <%= relation.related_to.name %> - <%= relation.discount_amount != 0 ? number_to_currency(relation.discount_amount) : "-" %> + <%= link_to relation.related_to.name, relation.related_to %> + <%= relation.discount_amount != 0 ? number_to_percentage(relation.discount_amount, :precision => 0) : "none" %> <%= relation.relation_type.name %> <%= link_to_delete relation, {:url => admin_product_relation_url(relation.relatable, relation)} %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 74d5cc15..d38736e8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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