// Add country calling code prefix in woocommerce billing phone
add_action( 'wp_footer', 'wpsh_add_callback' );
function wpsh_add_callback(){
?>
countries->get_country_calling_code( $country_code );
$calling_code = is_array( $calling_code ) ? $calling_code[0] : $calling_code;
}
echo $calling_code;
die();
}
// Phone number shoud be at least 6 digits long. This avoids the problem with not adding a phone number
add_action('woocommerce_checkout_process', 'wpsh_validate_phone');
function wpsh_validate_phone() {
if (isset($_POST['billing_phone'])) {
$phone = strlen(preg_replace('/[^0-9]/', '', $_POST['billing_phone']));
if ($phone < 6) {
wc_add_notice( __( 'Billing Phone must be at least 6 digits long.' ), 'error' );
}
}
}