Tech Tee Shirts Explained »

Why ZenCart Sales Tax is Error by a Penny





More Zen Cart books available on Amazon.

If you have encounter the problem where the ZenCart computed sales tax is off by a penny, it is because there is a round-off error bug in the ZenCart version 1.3.8 (and possibly before).  This issue and fix only applies to version 1.3.8 and not version 1.3.9.

ZenCart computed sales tax is correct in most cases. However, in certain cases involving orders with multiple products, there is a rounding error in the computed sales tax.  And the customer ends up paying one penny too much or too little. The bug is more likely to occur when the tax percentage is two decimal places to the right of the decimal point and then only occurs in certain combination of product prices.

One such combination of prices and tax that can reproduce this bug is the following...

1. Create a "taxable goods" product "Foobar" with price $3.95.
2. Create "taxable goods" product "Widget" with price $8.95.
3. Set California sales tax to 8.25%.
4. Have California customer purchase a Foobar and a Widget with product total of $12.90.
5. See that ZenCart is charging incorrect tax of $1.07
6. Expected result: The correct tax to be charged the customer is $1.06 obtained by (12.90 * 0.0825).

The reason why the ZenCart result is incorrect is because ZenCart is computing sales tax for each product, rounding, and then totalling. This is wrong. As learned in school, one should never round intermediary calculations.  One should round at the end just for display purposes.

The correct order of operation is to total all the product prices, then compute sales tax on that total, and then round for display.  This is also mentioned in "Texas Sales Tax FAQ" where in point number 2, it states "If you sell multiple taxable items on one invoice, then you must compute the tax on the total sum of the sales prices of the taxable items sold."

The way to fix this error is to alter core ZenCart code:

1. In file includes/functions/functions_taxes.php, remove the call to zen_round() from zen_add_tax() and
zen_calculate_tax() functions so that it now looks like ...

Fix ZenCart Sales Tax Error

2. In file includes/classes/order.php near the line number shown, remove the call to zen_round() so that it looks like ...

Fix zencart sales tax error

Note that these changes is applicable to version 1.3.8 of ZenCart.