I'm a php and wordpress newbie.
I have a woocommerce shop, where users are able to enter their phone number as [billing_phone] at woocommerce checkout page.
The mobile phones are 11 digits and they must include a ZERO at the beginning of it. Some users are adding the 0 to the beginning like 01234567891, while others simply do not.
What I want is to check the [billing_phone]s after the purchase process (after they recieve the Order #ID. and if it does not contain any 0 at the beginning add it automatically.
So if I go to Woocommerce> Orders> Order details> I should see an 11 digits with 0 under Billing> Phone number section.
I have read semi-related topics like Remove zero and understood that it can be done by using the preg_replace PHP functions.
and came up with a solution like
add_action('woocommerce_checkout_process', 'AddZero');
function AddZero() {
if (isset($_POST['billing_phone']) && strlen['billing_phone'] == 10)) { //added lenght check to make sure if it's 10 digits
$_POST['billing_phone'] = preg_replace('/\0/', '', $_POST['billing_phone']);
// $_POST['billing_phone'] = preg_replace('\*0\', '', $_POST['billing_phone']); //not working
// $_POST['billing_phone'] = preg_replace('/^0/', '0', $_POST['billing_phone']); // not working
// $_POST['billing_phone'] = preg_replace('/*0*/', '', $_POST['billing_phone']); //not working
// $_POST['billing_phone'] = preg_replace('/$0\d{9}*/', '', $_POST['billing_phone']); //not working
} }
then changed the if (isset($_POST['billing_phone']) && strlen['billing_phone'] == 10)) to if (!(preg_match('/[0-9]{10}*/', $_POST['billing_phone'] )))
nothing changed.
And finally tried what I learned from regex basic tutorial
function AddZero() {
if (isset($_POST['billing_phone'])) {
$_POST['billing_phone'] = preg_replace('/^0/', '0', $_POST['billing_phone']); //the ^ will spot the first position of the given string, and then add 0 there.
}
}
Not working either.
I don't know where I'm wrong, so any help is appreciated.
And please explain the answer if possible. I love to learn something new, not just using the given code.