Refactoring digital shipping calculation and display

* removed the deface override that modified the checkout/_delivery partial
* created custom digital delivery calculator
* removed digital_shipping_method from Spree::Order. Moved logic to available_shipping_methods monkey patch

The main goal was to eliminate the shipping method detection logic from the _delivery partial.
This was a bit of a hack all along and didn't really present the correct information to the underlying
order logic (i.e. rate_hash.blank? would return true when there was no shipping options available).
This should be more future proof and make overriding another aspect of the delivery process easier.
This commit is contained in:
Michael Bianco
2012-06-23 19:29:21 -04:00
parent 71340444c1
commit 049770056d
9 changed files with 64 additions and 72 deletions
@@ -0,0 +1,18 @@
# https://github.com/spree/spree/issues/1439
require_dependency 'spree/calculator'
module Spree
class Calculator::DigitalDelivery < Calculator::FlatRate
def self.description
I18n.t(:digital_delivery)
end
def compute(object=nil)
self.preferred_amount
end
def available?(order)
order.digital?
end
end
end
+13 -14
View File
@@ -1,26 +1,25 @@
Spree::Order.class_eval do
# Are all products/variants of this Order to be downloaded by the customer?
# all products are digital
def digital?
line_items.map { |item| return false unless item.digital? }
true
end
# Is at least one product/variant digital?
def some_digital?
line_items.map { |item| return true if item.digital? }
false
end
# Determine which method to use for shipping of digital products.
def digital_shipping_method
rates = rate_hash
# If there is a shipping method has "Download" in its name then we take that one.
rates.each { |rate| return rate if rate[:name].downcase.include?('download') }
# Other than that, we take the first one that we find that doesn't cost anything.
rates.each { |rate| return rate if rate[:cost] == 0 }
# Well, at this point we have a problem. No shipping method is cost-free or called "download".
nil
end
# TODO this works as of spree 1.1.1; make sure to check the original function on upgrade
def available_shipping_methods(display_on = nil)
return [] unless ship_address
all_methods = Spree::ShippingMethod.all_available(self, display_on)
if self.digital?
all_methods.detect { |m| m.calculator.class == Spree::Calculator::DigitalDelivery }.try { |d| [d] } || all_methods
else
all_methods
end
end
end
-1
View File
@@ -1,4 +1,3 @@
Spree::Product.class_eval do
has_many :digitals, :through => :variants_including_master
end
@@ -1,39 +0,0 @@
Deface::Override.new(:virtual_path => "spree/checkout/_delivery",
:name => "modify_shipping_options_display",
:replace_contents => "#shipping_method #methods p.radios",
:original => %q{
<% @order.rate_hash.each do |shipping_method| %>
<label>
<%= radio_button(:order, :shipping_method_id, shipping_method[:id]) %>
<% if Spree::Config[:shipment_inc_vat] %>
<%= shipping_method[:name] %> <%= format_price (1 + Spree::TaxRate.default) * shipping_method[:cost] %>
<% else %>
<%= shipping_method[:name] %> <%= number_to_currency shipping_method[:cost] %>
<% end %>
</label>
<% end %>
},
:text => %q{
<% if @order.digital? && @order.digital_shipping_method.present? %>
<label>
<%= radio_button :order, :shipping_method_id, @order.digital_shipping_method[:id] %>
<%== t 'digital_shipping', :email => @order.email %> (<%= number_to_currency @order.digital_shipping_method[:cost] %>)
</label>
<% else %>
<% filtered_rate_hash = @order.rate_hash.select { |m| !(@order.digital_shipping_method && m[:id] == @order.digital_shipping_method[:id]) } %>
<% if filtered_rate_hash.count > 0 %>
<% filtered_rate_hash.each do |shipping_method| %>
<label>
<%= radio_button(:order, :shipping_method_id, shipping_method[:id]) %>
<% if Spree::Config[:shipment_inc_vat] %>
<%= shipping_method[:name] %> <%= format_price (1 + Spree::TaxRate.default) * shipping_method[:cost] %>
<% else %>
<%= shipping_method[:name] %> <%= number_to_currency shipping_method[:cost] %>
<% end %>
</label>
<% end %>
<% else %>
<%= t :no_shipping_methods %>
<% end %>
<% end %>
})