The Problem

We have very specific (zip-code level) tax rates for the state of Ohio. As a result, we had about 1,500 tax rates that we needed to apply to a specific rule. Importing the tax rates went well. However, whenever I'd try to the apply tax rates to the rule, I'd receive a ""Code Already Exists"" error.

The Solution

Magento's error isn't very helpful, but after some digging I found that the problem was actually in my PHP configuration. max_input_vars is set to 1,000 by default. Because we were adding more than 1,000 variables the POST data was truncated.

To fix this problem, add max_input_vars = 2500 (or modify) to your php.ini configuration file and restart Apache. Obviously, set max_input_vars according to your needs.

Magento Code Already Exists Error

But I Can't change my PHP settings!

In the event that you are unable to change your PHP settings and your host isn't helpful, you can associate your tax rates with your rules manually. The caveat to this is that if someone ever visits the rule page displayed above and attempts to save the tax rates (assuming there are still too many), you're going to have to run the manual update again.

When you attempt to save your rates and there are too many, the save action will break at a particular number (in other words, it is not random - the last say, 100 rates simply won't be added to the database). For example, in my case, I had 1428 rates, and the saving stopped at record 999.

Check out the tax_calculation table and look at the highest tax_calculation_rate_id saved. This column is a foreign key that points to the record id in the tax_calculation_rate table. For example, in my case the tax_calculation_rate_id truncated at 999.

The tax_calculation_rule_id, customer_tax_class_id and product_tax_class_id were all the same. Obviously the tax_calculation_rule_id refers to the rule that we are associating these rates and the other two are constant as well.

Inserting the Records

There are inevitably several ways to do this, but I simply opened Excel and set up columns for tax_calculation_rate_id, tax_calculation_rule_id, customer_tax_class_id and product_tax_class_id.

I set up increment formula to start at 1000 for the tax_calculation_rate_id and add one to each row up to 1428. (429 rows) The other columns were set to their respective constants (1, 3 and 2 in my case).

Copying all of the columns into SublimeText gave me everything I needed to quickly do some mass replacements and develop the necessary MySQL query to insert references to these rates.

For example:

INSERT INTO `tax_calculation` ( `tax_calculation_rate_id`, `tax_calculation_rule_id`, `customer_tax_class_id`, `product_tax_class_id`)
(1000, 1, 3, 2),
(1001, 1, 3, 2),
(1002, 1, 3, 2),
(1003, 1, 3, 2),
(1004, 1, 3, 2)
(1428, 1, 3, 2);

Make a backup of your DB first in case anything goes wrong and run the query. Navigate to your applicable tax rule in the Admin panel and make sure that all of the desired rates are highlighted. Again, look, but don't save or you might mess up everything you just did!

Admittedly, the best solution is to have the desired max_input_vars setting in the first place, but in a pinch, the above will work.

Blog Logo

Phil Birnie



Phil Birnie

Full Stack Web Developer from Columbus, Ohio

Back to Overview