Woocommerce Taxes Not Working? Here’s the Simplest of All Solutions
I recently had an issue with Woocommerce’s built in taxes where they were just not working correctly. Only customers with a shipping address in Pennsylvania were supposed to get charged tax, and it was supposed to read “State Tax”, charging 6% on their order total.
But people in other states were randomly being charged tax. I investigated various possibilities, such as whether the Jetpack / Woocommerce Services (though deactivated and removed from the site) was somehow lingering around, (it wasn’t, though I still find it annoying how Automattic tries to get you to “automatically” add those plugins). I spoke with Flywheel support (my hosting company) to see if it could be a caching issue with their system. I spoke several times with Woocommerce. Nothing helped.
We only needed to charge tax for PA shipping addresses, so I circumvented the whole thing like so:
// let's do taxes ourselves
add_action( 'woocommerce_cart_calculate_fees','custom_woo_tax' );
function custom_woo_tax() {
global $woocommerce;
if (is_admin() && ! defined( 'DOING_AJAX' ))
return;
$state = array('PA');
$cart_total_dollars = WC()->cart->cart_contents_total;
$tax = $cart_total_dollars * .06;
if (in_array(WC()->customer->shipping_state, $state)) {
$woocommerce->cart->add_fee( 'PA Sales Tax', $tax, true, '' );
}
}
And that solved the problem! I can’t speak to what the original issue was, but if you only need simple tax settings, this could work quite well. You could expand on it, too, in case you needed to charge tax in multiple states, countries, etc. Just do some conditionals on the location and take it from there!
Up Next: Why "Value Based" Pricing is a Scam