I have a 3 shipping methods in my Woocommerce store and I want to disable the tax rate when customer choose the 3rd shipping method.
How can I do that?
I have a 3 shipping methods in my Woocommerce store and I want to disable the tax rate when customer choose the 3rd shipping method.
How can I do that?
As settings doesn't seem to be working to remove taxes from specific shipping methods, try the following code that will set zero taxes to your specific defined shipping methods:
add_filter('woocommerce_package_rates', 'null_specific_shipping_method_taxes', 12, 2);
function null_specific_shipping_method_taxes( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE define your targeted shipping methods IDs (in this array)
$shipping_methods_ids = array( 'flat_rate:2', 'flat_rate:12', 'flat_rate:3', 'shipping_by_rules:5' );
// Loop through shipping rates
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Only for your defined Shipping method IDs
if( in_array( $rate->id, $shipping_methods_ids ) ){
$taxes = [];
// Loop through the shipping taxes array (as they can be many)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $rates[$rate_key]->taxes[$key] > 0 ){
// Set each tax cost to zero
$taxes[$key] = 0;
$has_taxes = true;
}
}
// Set the new taxes array
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
return $rates;
}
This code goes on function.php file of your active child theme (or theme). Tested and works (if settings are made in a correct way, regarding related shipping methods and taxes in Woocommerce)…