Fixing Zen Cart’s Tax Miscalculation When Using a Coupon

I was recently alerted to an error in Zen Cart by a client where the tax was being miscalculated when using a coupon. Here’s the fix.

Around line 240 in /store/includes/modules/order_total/ot_coupon.php

Remove these lines:

//$od_amount[$tax_desc] += (($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate)/100 * $ratio;
//$od_amount[$tax_desc] += round(((($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate) + .5)/100 * $ratio, 2);

Replace with this:

//hack by Cameron P. to fix the coupon tax errors.
//for some reason the tax was off by the number of products in the cart times the tax (not quantity)
$od_amount[$tax_desc] += round(((($products[$j]['final_price'] * $products[$j]['quantity']) * $tax_rate) )/(100*sizeof($products)) * $ratio, 3);

I did a few things here:
* The tax was off by the tax total times the number of products in the cart (independent of quantity), so that’s why the sizeof() bit is in there
* Removed the + 0.5 addition to the total. Why that’s even in there to begin with, I don’t know. It was making some of my totals come out wrong by a few cents here and there. The new equation seems to work better without it.
* Extended the price to three significant figures. Probably not 100% necessary, but it seemed to help fix some of the issues where the total was off by a penny or two.

    Hope this helps!

    Leave a comment

    Hey there! Come check out all-new content at my new mistercameron.com!