Spree Commerce

Try It Now

"New on Edge: Charges and Credits"

Posted on July 20, 2009 by Sean Schofield

Spree now provides a new flexible system of charges and credits associated with orders. The orders table no longer has seperate columns for tax_total, ship_total, etc. This information is now captured more generically as a Charge. This allows a Spree application to add more then one tax or shipping charge per order as well as to support new types of charges that might be required. For instance, some products for sale (like cell phones) require a seperate activation fee.

Charges are added to the order in the Checkout model. Developers wishing to override the default behavior of Spree can now just override the update_charges method to provide their own additional logic. This snippet below shows how the Spree core adds tax related charges to an order.


def update_charges
…
<ol>
	<li>update tax (if applicable)<br />
    tax_amount = order.calculate_tax<br />
    if tax_amount &gt; 0<br />
      tax_charge = order.tax_charges.first<br />
      tax_charge ||= order.tax_charges.build(:description =&gt; I18n.t(:tax))<br />
      tax_charge.amount = tax_amount<br />
      tax_charge.save<br />
    end</li>
</ol>
order.reload
order.update_totals
order.save
end

In addition to charges, credits are now supported. Credits are the mirror image of charges and they serve to reduce the cost of the order. The primary mechanism for adding a credit to an order is through the use of coupons. Its possible, however, that developers may wish to build their own custom logic for creating credits.

WARNING: There are significant changes to the database to support the new charge system. The migrations should update your legacy data and create the necessary tax and shipping charges for existing orders but you should backup your database before running.