Have you come across the issue where you have used an SMS plugin for WooCommerce however, the messages simply couldn’t be sent to the customers. The root cause of this issue being the missing country code prefix in your customers’ phone numbers as required by your preferred SMS gateway.

WooCommerce, by default, doesn’t specifically ask customers to enter their country code prefix while entering their phone numbers on the registration form. So how can we overcome this situation so that the customers can still be sent SMS’s without any issue?

WooCommerce has a hook called ‘woocommerce_checkout_process’. By plugging in your custom function to this hook you can modify the customer phone number to dynamically add the country code prefix. The following code does the same thing. Here +44 is the country code for UK.


// Prefix country code to customer phone number
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {

// Add +44 as prefix to buyer phone number
$number_length = strlen($_POST['billing_phone']);

if($number_length == 11) {
// Remove first char if it is 0 and add +44
if($_POST['billing_phone'][0] == 0) {
$_POST['billing_phone'] = substr($_POST['billing_phone'], 1);
$_POST['billing_phone'] = '+44'.$_POST['billing_phone'];
}
} elseif($number_length == 10) {
$_POST['billing_phone'] = '+44'.$_POST['billing_phone'];
}
}