Woocommerce – Shipping tax class based on cart items not using the highest tax available

I have found the answer by override the shipping tax filter

// 0% and 21% tax producdts added combined to the cart needs to have 21% shipping tax
add_filter('woocommerce_shipping_packages', 'override_woocommerce_shipping_packages');
function override_woocommerce_shipping_packages($packages) {
    $shipment_needs_tax = false;
    foreach ($packages[0]['contents'] as $cartitem) {
        if (!empty($cartitem['data']->tax_class)) $shipment_needs_tax = true;
    }

    if ($shipment_needs_tax && empty($packages[0]['rates']['flat_rate:3']->get_taxes())) {
        $shipcost = $packages[0]['rates']['flat_rate:3']->get_cost();
        $shiptax = $shipcost * 0.21;
        if ($shiptax > 0) $packages[0]['rates']['flat_rate:3']->set_taxes([4 => $shiptax]);
    }

    return $packages;
}