Every repository with this icon (
Every repository with this icon (
How to provide free or multi-rate flat shipping.
If you would like to offer free shipping, or say one flat rate for 2nd day air, and one for overnight, you can do this easily.
First follow the customization guide where you create a “site” extension.
In RAILS_ROOT/vendor/extension/site/lib/foo.rb place the following code:
module Foo module FreeShipping class Calculator def calculate_shipping(shipment) 0 end end end module AirShipping class Calculator def calculate_shipping(shipment) 20 end end end module OvernightShipping class Calculator def calculate_shipping(shipment) 30 end end end end
You will need to re-start your app.
You may now create new shipment types and set their calculator classes to Foo::FreeShipping::Calculator, or Foo::AirShipping::Calculator, or Foo::OvernightShipping::Calculator.
This worked for me initally, but I seem to be having problems after trying to use all three. I get a server error during checkout.
Your mileage may vary.
UPDATE: My problem was because I then deleted the OLD shipping methods as we don’t offer them. Because the shipping_method.id is stored in the shipments table at time of cart creation, this breaks ALL old carts, orders, and is a bad thing. If you have to delete your old shipping methods I suggest you close out any existing orders (mark as shipped), then go and add the new shipment methods as above. Then enter the database and update ALL existing shipping records to the id of the new one or ones. This way you can still bring up your old records. Kludgy, but it’ll get ya by until the issue with deleting shipping methods causing db inconsistencies is fixored.
UPDATE 2:
The calculator method described in this document only partially works due to a bug in the gem 0.8.3, and perhaps others, in checkout.rb. Basically no matter what shipping option is chosen, only the first in the database is applied. This can be remedied by this hack.
a) Copy lib/spree/checkout.rb into your site extension.
b) Add the following dirty hack.
## Add hack under this line
@order.shipments.build(:address => @order.ship_address, :shipping_method => @shipping_method) if @order.shipments.empty?
## DIRTY HACK -- RR214
shipment = @order.shipments[0]
shipment.shipping_method = ShippingMethod.find_by_id params[:method_id] unless params[:method_id].nil?
shipment.save
## END HACK
As of 9/11/2009 this bug is also visible on demo.spreecommerce.com. Even though all the shipping prices are the same, notice when you checkout with the magic CC number 411111111111111 you’ll only get one shipping option listed in the ()’s Not the one you selected.






