I have to display phone as required (mandatory) only if a certain country is selected in WooCommerce checkout screen.
What is the validation rule to check the selected country in real time?
I have tried the following code, which is working for making phone non required:
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
You mostly need to use javascript for real time events or live events on client side… The code below is mostly using jQuery and a bit of PHP, to make the billing phone required only when customer select specific countries:
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works.