'Set all shipping taxes to zero WooCommerce

I have this function to check if a logged in user has a ACF of gratis verzending checked so based on this I set the shipping rate on 0.

add_filter('woocommerce_package_rates','free_shipping_for_customer',100,2);
function free_shipping_for_customer($rates, $package) {

    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    $free_shipping = get_field('gratis_verzending', 'user_'.$current_user_id.'');
    
    if ($free_shipping) {
        
        foreach ($rates as $rate) {
            //Set the price
            $rate->cost = 0;
        }
        
    }
    return $rates;
}

My problem is that the shipping tax is still a value. If user has free shipping the taxes of all shipping should be 0. How can I set all the shipping taxes on 0?

FOUND THE SOLUTION:

add_filter('woocommerce_package_rates', 'shipping_cost_based_on_weight', 12, 2);
function shipping_cost_based_on_weight( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;
    
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    $free_shipping = get_field('gratis_verzending', 'user_'.$current_user_id.'');

    if ($free_shipping) {
        // Loop through the shipping taxes array
        foreach ( $rates as $rate_key => $rate ){
            $has_taxes = false;
                // Set the new cost
                $rates[$rate_key]->cost = 0;
                // Taxes rate cost (if enabled)
                $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 the new tax cost
                        $taxes[$key] = 0;
                        $has_taxes   = true; // Enabling tax
                    }
                }
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
        }
    }
    return $rates;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source