Tax rates based on zip codes in Drupal Commerce

August 15, 2011Comments (1)Printer-friendly versionPrinter-friendly version

Recently I have been working on a new e-commerce site for Onyx Group. The site, everfan.com, uses the latest version of Drupal, in addition to using the newest version of Drupal Commerce. Formerly known as Ubercart, Drupal Commerce has been completely revamped to provide a more modular framework for developers. Although there is currently a lot of development happening on Drupal Commerce and some other supporting modules, it isn't quite ready for an out-of-the-box storefront just yet. However, if you are a developer with a bit of knowledge, you can currently take advantage of Drupal Commerce and roll your own solution.

One of the issues that we ran into while developing everfan.com is the fact that Drupal Commerce's tax system isn't yet set up to support an easy way to query tax information from the database. We needed to set up a tax system that supported different tax rates for different municipalities in Georgia. For instance, if we ship to one zip code in Georgia it may have a different tax rate than another. This tutorial shows you how to set up conditional taxes, however it would have been far too much work to manually input a whole table full of taxes based on zip code (There are about 1000 different zip codes).

So rather than input these tax rates into the rules system, I decided to directly hook into Drupal Commerce's tax processing so that we could just query the database based upon the current order's shipping zip code. In order to accomplish this, I needed to install the Drupal Commerce Shipping module first and update all of our Drupal Commerce code to the latest versions. Then I created a database table that included a list of all of the Georgia zip codes and tax rates that I would query from. Next I used hook_commerce_tax_rate_info() to process the tax info by checking the zip code of the current order's shipping address, pulling the correct tax rate from the database, and then applying that tax rate to the order. I was really happy to see how easy it is to hook into commerce's tax system, and I can foresee that developing more complex tax rate calculations shouldn't be too difficult.

Note: All of the code for our module is available on OnyxGroup's GitHub site.

Related Links

Back to my blog
Adrian Mummey
Posted by: Adrian Mummey
Posted on: August 15, 2011
Ryan writes:
posted: 08.15.11

Glad to hear it worked out for you. I've been itching to get some "real world" feedback on those tax hooks for a while. : )

Post a comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Image CAPTCHA
Enter the characters shown in the image.
Adrian Mummey

you should know

Adrian is a programmer and web development expert living on the beach in Ventura, CA. He is currently attending California State University Channel Islands and is pursuing an MS in Computer Science. He recently returned to California after a long stint in Mongolia. He travelled to Mongolia as a Peace Corps volunteer in 2004, where he worked as a teacher at a rural school until 2006.

His specialties in development are PHP, MySQL, Javascript and CSS and he is always looking for interesting programming projects and enjoys producing Open Source Software. Adrian is a Zend PHP 5 certified engineer as well as an AB MySQL certified developer.

you should follow

Just completed a 28.37 mi bike with @RunKeeper. Check it out! http://t.co/ZKpHNwJd #RunKeeper - Twitter 23 days ago

you should love

  • Is currently training for Las Vegas Marathon
  • Honed his amazing web skills in a yurt on the frigid steppe of Mongolia.
  • Climbed Mt. Kilimanjaro.
  • Can give a culturally appropriate “Cheers” in over 10 languages.
  • His international port of Joomla! was localized in nine countries.
  • Is currently pursuing his lifelong goal of living on the beach.
  • Graduated from California Polytechnic State University, San Luis Obispo, B.S. Computer Science.

you should see