I've been reading on some of the threads in this forum regarding this issue, and I believe I'm getting close to a "hack" to get this to work. Since there is no way to narrow a zone down to zip code, the only way to set up this functionality is to bypass the built-in taxing function entirely.
The following guide is assuming you are willing to bypass the zone functionality, and every item in your store will be taxed the same way.
-----------------
To get the system ready, set it up to not add tax to the displayed prices of products, and set up a dummy tax item which you'll apply to every product (could be any zone and rate). We won't actually be using this to apply tax, it's just there so the system will know we need to tax these items.
----------------
The first step is to make zip code a required field in the customer account setup:
in catalog/controller/account/create.php
around line 96, add the following to display the error message if field fails check:
Code: Select all
$this->data['error_postcode'] = @$this->error['postcode'];
Code: Select all
if ((strlen(utf8_decode($this->request->post['postcode'])) < 5) || (strlen(utf8_decode($this->request->post['postcode'])) > 5)) {
$this->error['postcode'] = $this->language->get('error_postcode');
}
Under the error section, add the following to display a message:
Code: Select all
$_['error_postcode'] = 'Post code must be 5 digits!';
So, that takes care of the zip code field. We know we'll get that when they sign up. Now we need a database. You'll have to find one that works for your state. For my case, I needed NY, and found a great one (ironically) under the addon site for oscommerce:
http://addons.oscommerce.com/info/2792
create a separate database with your zip, rate, (and county if you want) fields.
-----------------------
Now we're checking for the zip code, we have tax rates for the zip code, now we just need to apply them at checkout. The way I see it, there's no need to reinvent the tax system. It's too easy in PHP to lookup rates on our own database, and add them to the total. Only question is the best place to do that. I think I've found that. It's the file that's called on the confirmation screen that totals up the tax rates and updates the total:
catalog/model/total/tax.php
Code: Select all
<?php
class ModelTotalTax extends Model {
public function getTotal(&$total_data, &$total, &$taxes) {
if ($this->config->get('tax_status')) {
foreach ($taxes as $key => $value) {
if ($value > 0) {
$total_data[] = array(
'title' => $this->tax->getDescription($key) . ':',
'text' => $this->currency->format($value),
'value' => $value,
'sort_order' => $this->config->get('tax_sort_order')
);
$total += $value;
}
}
}
}
}
?>
Code: Select all
$xdbhost = 'localhost';
$xdbuser = 'USER';
$xdbpass = 'PASS';
$xconn = mysql_connect($xdbhost, $xdbuser, $xdbpass) or die
('Error connecting to mysql');
$xdbname = 'DATABASE';
mysql_select_db($xdbname);
$zipcode="11530";
$xresult=mysql_query("SELECT * FROM ziptax WHERE zip_code=$zipcode");
$xrow=mysql_fetch_array($xresult);
$xtaxrate=$xrow['zip_tax_rate'];
mysql_close($conn);
1. I need to get the zip code from the current order/customer in order to pull the right rate. Right now I'm just hard coding it (11530) How can I do that?
2. I need to get the total quantity of items in the cart, so I can display the correct tax on the order, and add the proper amount to the total price.
Once I have these two things figured out, I believe some simple modifications to this script will allow for taxing by zip code. Similar edits could be done to the order history and/or invoice pages.
Any thoughts / ideas?