Blog

Posts Tagged ‘shipping’

August 13 2009

Polymorphic Caclulatorsby railsdog

There has been significant refactoring to the implementation of calculators. Calculators are now polymorphic and belong to calculable. This will have a non trivial impact on your existing store configuration. After upgrading to Spree 0.9.0 you are likely going to have to make several manual adjustments to the existing tax and shipping configurations. Ultimately we feel this is outweighed by the superior design of the new calculator system which will allow for a more modular design.

WARNING Many of the existing calculator extensions are not yet updated to support Spree 0.9.0. Please check the extension registry to see which versions are supported. Our goal is to backport most of the useful calculators shortly after the release.

All calculators need to implement the following method


  def compute(something=nil)
  end    

The calculator is passed an optional "target" on which to base their calculation. This method is expected to return a single numeric value when the calculation is complete. A value of nil should be returned in the event that a charge is not applicable.

Calculators are also configurable thorugh Spree's flexible preference system. Default values for the preferences are configured through the class definition. For example, the flat rate calculator class definition specifies an amount with a default value of 0.


  class Calculator::FlatRate < Calculator
    preference :amount, :decimal, :default => 0
    ...
  end

Spree now contains a standard mechanism by which calculator preferences can be editied. The screenshot below shows how the amounts for the flat rate calculator are now editable directly in the admin interface.

shipping_config

June 25 2009

Checkout Now Supports Flexible System of Chargesby railsdog

The edge version of Spree has just been refactored to allow support for a new and improved system of order charges. This recent commit gives you the ability to have an arbitrary number of charges associated with the order. You are no longer restricted to just shipping and tax amounts (although those are still supported as charges now.) This allows your checkout logic to add your own custom fees (and descriptions) if needed.

There is a new rake task which should migrate your existing orders so that they now have shipping and tax charges when applicable. As always, please backup your database before attempting to run the migrations. If you're running the source you'll also need to remove the now empty vendor/extensions/tax_calculator directory after your git pull.

This code is very edge so be prepared for a few bumps if you decide to use it. There is at least one known issue and we'll likely encounter a few more as we go. You may also want to do a search in your custom code for references to tax_amount and ship_amount. These are no longer valid attributes for the order model. If you need the tax or shipping total for something, however, you can do the following:


  @order.tax_charges.sum(:amount)

So now that we can support an arbitrary number of charges, the next task is to support an arbitrary number (and type) of credits. This concept should be relatively easy since a credit is really just the flip side of a charge. Once that is implemented we can finally get to the coupon and discount stuff that we have been planning for a while now.