Skip to content

Instantly share code, notes, and snippets.

@webdados
Last active April 17, 2018 18:59
Show Gist options
  • Save webdados/139079c9e654ed2d8418ed500bdcc778 to your computer and use it in GitHub Desktop.
Save webdados/139079c9e654ed2d8418ed500bdcc778 to your computer and use it in GitHub Desktop.
Fix tax rounding problems in WooCommerce
WooCommerce stores tax subtotals rounded with the same number of decimals that are set for the store, which can cause problems when invoicing on external services
Example when using 2 decimals and tax 23%:
Total itens (without tax): 68.24
Itens tax (rounded with 2 decimals): 15.6952 = 15.70
Shipping (without tax): 4.73
Shipping tax (rounded with 2 decimals): 1.0879 = 1.09
Total taxes: 16.79
Total: 72.97 + 16.79 = 89.76
How most ERPs would calculate taxes:
Itens tax: 15.6952
Shipping tax: 1.0879
Total taxes (rounded with 2 decimals, at the end): 16.7831 = 16.78
Total: 72.97 + 16.79 = 89.75
To avoid this, we can use the 'wc_round_tax_total' filter to force taxes to be stored and calculated without rounding
<?php
add_filter( 'wc_round_tax_total', 'fix_wc_round_tax_total', 10, 4 );
function fix_wc_round_tax_total( $rounded_tax, $value, $precision, $WC_TAX_ROUNDING_MODE ) {
return( $value );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment