Remove Billing Fields if Cart Contains Only Virtual Products

add_filter('woocommerce_checkout_fields', 'remove_billing_on_virtual');
function remove_billing_on_virtual($fields) {
    $only_virtual = true;

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        if (!($cart_item['data']->is_virtual())) $only_virtual = false;
    }

    if ($only_virtual) {
        unset($fields['billing']);
    }

    return $fields;
}