Permalink
Browse files

Merge branch 'implement_discounts'

Conflicts:
	README.md
	spree_related_products.gemspec
  • Loading branch information...
2 parents 5921bcb + 756c323 commit 539ca7f0a15dd9f57b63339483a6756d3e1bccd4 @BDQ BDQ committed Feb 2, 2012
View
@@ -40,8 +40,9 @@ You can access all related products regardless of RelationType by:
=> []
Discounts
----------
-Currently, master doesn't support discounts out of the box. There is however a branch __implement_discounts__ that should have the necessary discount-enabled code (which was cherry-picked from the working 0-70-x branch).
+You can optionally specify a discount amount to be applied if a customer purchases both products.
+
+Note: In order for the coupon to be automatically applied, you must create a promotion leaving the __code__ value empty, and adding an Action of type : __RelatedProductDiscount__ (blank codes are required for coupons to be automatically applied).
Installation
------------
@@ -1,16 +1,11 @@
module Spree
- class RelatedProductDiscount < Spree::Calculator
+ class Calculator::RelatedProductDiscount < Spree::Calculator
preference :item_total_threshold, :decimal, :default => 5
def self.description
I18n.t("related_product_discount")
end
- def self.register
- super
- Spree::Coupon.register_calculator(self)
- end
-
def compute(object)
if object.is_a?(Array)
return if object.empty?
@@ -21,12 +16,12 @@ def compute(object)
return unless eligible?(order)
total = order.line_items.inject(0) do |total, line_item|
- relations = Spree::Relation.find(:all, :conditions => ["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Product", line_item.variant.product.id])
+ relations = Spree::Relation.find(:all, :conditions => ["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Spree::Product", line_item.variant.product.id])
discount_applies_to = relations.map {|rel| rel.related_to.master }
order.line_items.each do |li|
if discount_applies_to.include? li.variant
- discount = relations.detect {|rel| rel.related_to.variant == li.variant}.discount_amount
+ discount = relations.detect {|rel| rel.related_to.master == li.variant}.discount_amount
total += if li.quantity < line_item.quantity
(discount * li.quantity)
@@ -43,7 +38,7 @@ def compute(object)
end
def eligible?(order)
- order.line_items.any? { |line_item| Spree::Relation.exists?(["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Product", line_item.variant.product.id])}
+ order.line_items.any? { |line_item| Spree::Relation.exists?(["discount_amount <> 0.0 AND relatable_type = ? AND relatable_id = ?", "Spree::Product", line_item.variant.product.id])}
end
end
@@ -2,9 +2,7 @@
<thead>
<tr>
<th><%= t('name') %></th>
- <% if false # Spree::Extension.descendants.map(&:to_s).include? "AutomaticCouponsExtension" %>
- <th><%= t('discount_amount') %></th>
- <% end %>
+ <th><%= t('discount_amount') %></th>
<th><%= t('type') %></th>
<th></th>
</tr>
@@ -13,9 +11,7 @@
<% product.relations.each do |relation| %>
<tr id="<%= dom_id relation %>">
<td><%= relation.related_to.name %></td>
- <% if false #Spree::Extension.descendants.map(&:to_s).include? "AutomaticCouponsExtension" %>
- <td><%= relation.discount_amount != 0 ? number_to_currency(relation.discount_amount) : "-" %></td>
- <% end %>
+ <td><%= relation.discount_amount != 0 ? number_to_currency(relation.discount_amount) : "-" %></td>
<td><%= relation.relation_type.name %></td>
<td width="70px">
<%= link_to_delete relation, {:url => admin_product_relation_url(relation.relatable, relation)} %>
@@ -16,13 +16,10 @@
<%= text_field_tag :add_product_name, '', :class => 'fullwidth title' %>
<%= hidden_field_tag :add_variant_id %>
</div>
- <!-- Needs to be updated once Automatic Coupons extension is migrated to 0.30.x -->
- <% if false #Spree::Extension.descendants.map(&:to_s).include? "AutomaticCouponsExtension" %>
- <div style="float:left;width:10%;margin-right:5%;">
- <%= label_tag :add_discount, t("discount_amount") %>
- <%= text_field_tag :add_discount, 0.0, :style => 'margin: 0pt; padding: 4px; font-size: 1.5em; width: 98%;' %>
- </div>
- <% end %>
+ <div style="float:left;width:10%;margin-right:5%;">
+ <%= label_tag :add_discount, t("discount_amount") %>
+ <%= text_field_tag :add_discount, 0.0, :style => 'margin: 0pt; padding: 4px; font-size: 1.5em; width: 98%;' %>
+ </div>
<div style="float:left;width:20%;margin-right:5%;">
<%= label_tag :add_type, t("type") %>
<%= select_tag :add_type, options_for_select(@relation_types.map{|rt| [rt.name, rt.id]}), :style => 'margin: 0pt; padding: 4px; font-size: 1.5em; width: 98%;' %>
@@ -12,6 +12,10 @@ def self.activate
end
+ initializer "spree.promo.register.promotion.calculators" do |app|
+ app.config.spree.calculators.promotion_actions_create_adjustments << Spree::Calculator::RelatedProductDiscount
+ end
+
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/models/spree/calculator)
config.to_prepare &method(:activate).to_proc

0 comments on commit 539ca7f

Please sign in to comment.