This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
forked from amw/spree-simple-volume-pricing
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of changing price subtract discount from order total.
This is a first step to allow us to apply discount differently to quantity portions of the same variant. I.e. sell 3 first units for $10, the next three for $9 and all the next for $8.
- Loading branch information
Adam Wróbel
committed
Mar 3, 2011
1 parent
49190d2
commit 21caba7
Showing
12 changed files
with
155 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,10 +1,31 @@ | |||
LineItem.class_eval do | LineItem.class_eval do | ||
before_update :check_volume_pricing | before_save :check_update_volume_discount | ||
|
|
||
private | def update_volume_discount updated_order = nil | ||
def check_volume_pricing | self.volume_discount = 0 | ||
if changed? && changes.keys.include?("quantity") |
|
||
self.price = variant.volume_price quantity, order | return if self.quantity < 1 || variant.volume_prices.empty? | ||
|
|||
self.order = updated_order if updated_order | |||
total_quantity = self.quantity + order.variant_starting_quantity(variant) | |||
final_price = default_price = self.price | |||
|
|||
variant.volume_prices.each do |vp| | |||
break if vp.starting_quantity > total_quantity | |||
final_price = vp.price | |||
end | end | ||
|
|||
self.volume_discount = self.quantity * (final_price - default_price) | |||
end | |||
|
|||
def amount_with_volume_discount | |||
update_volume_discount | |||
amount_without_volume_discount + self.volume_discount | |||
end | |||
alias_method_chain :amount, :volume_discount | |||
|
|||
private | |||
def check_update_volume_discount | |||
update_volume_discount if price_changed? || quantity_changed? | |||
end | end | ||
end | end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,17 @@ | |||
Dear Customer, | |||
|
|||
Your order has been CANCELED. Please retain this cancellation information for your records. | |||
|
|||
============================================================ | |||
Order Summary [CANCELED] | |||
============================================================ | |||
<% for item in @order.line_items %> | |||
<%=item.variant.sku %> <%=item.variant.product.name%> <%= variant_options(item.variant) %> (<%=item.quantity%>) @ <%= number_to_currency item.price %> = <%= number_to_currency(item.price * item.quantity) %> | |||
<% end %> | |||
============================================================ | |||
Volume Discount: <%= number_to_currency @order.volume_discount %> | |||
Subtotal: <%= number_to_currency @order.item_total %> | |||
<% @order.adjustments.each do |adjustment| %> | |||
<%= "#{adjustment.label}: #{number_to_currency adjustment.amount}"%> | |||
<% end %> | |||
Order Total: <%= number_to_currency @order.total %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
Dear Customer, | |||
|
|||
Please review and retain the following order information for your records. | |||
|
|||
============================================================ | |||
Order Summary | |||
============================================================ | |||
<% for item in @order.line_items %> | |||
<%=item.variant.sku %> <%=item.variant.product.name%> <%= variant_options(item.variant) %> (<%=item.quantity%>) @ <%= number_to_currency item.price %> = <%= number_to_currency(item.price * item.quantity) %> | |||
<% end %> | |||
============================================================ | |||
Volume Discount: <%= number_to_currency @order.volume_discount %> | |||
Subtotal: <%= number_to_currency @order.item_total %> | |||
<% @order.adjustments.each do |adjustment| %> | |||
<%= "#{adjustment.label}: #{number_to_currency adjustment.amount}"%> | |||
<% end %> | |||
Order Total: <%= number_to_currency @order.total %> | |||
|
|||
|
|||
Thank you for your business. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,18 @@ | |||
<% if @order.volume_discount != 0.0 %> | |||
<style> | |||
#cart-volume-discount { | |||
float: right; | |||
width: 30%; | |||
text-align: left; | |||
} | |||
div#subtotal { | |||
clear: both; | |||
width: 30%; | |||
} | |||
</style> | |||
<div id="cart-volume-discount"> | |||
<h3> | |||
<%= t 'volume_discount' %>: <%= format_price @order.volume_discount %> | |||
</h3> | |||
</div> | |||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
<% if @order.volume_discount != 0.0 %> | |||
<tbody id='volume-discount'> | |||
<tr class="total" id="volume-discount-row"> | |||
<td colspan="3"><b><%= t('volume_discount') %>:</b></td> | |||
<td class="total"><span><%= number_to_currency @order.volume_discount %></span></td> | |||
</tr> | |||
</tbody> | |||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,5 +1,6 @@ | |||
en: | en: | ||
volume_pricing: Volume Pricing | volume_pricing: Volume Pricing | ||
volume_prices: Volume Prices | volume_prices: Volume Prices | ||
volume_discount: Volume Discount | |||
starting_from: Starting from | starting_from: Starting from | ||
add_volume_price: Add Volume Price | add_volume_price: Add Volume Price |
10 changes: 10 additions & 0 deletions
10
db/migrate/20110302170148_add_volume_discount_to_line_items.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,10 @@ | |||
class AddVolumeDiscountToLineItems < ActiveRecord::Migration | |||
def self.up | |||
add_column :line_items, :volume_discount, :decimal, | |||
:precision => 8, :scale => 2, :null => false, :default => 0 | |||
end | |||
|
|||
def self.down | |||
remove_column :line_items, :volume_discount | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters