Use customer’s phone number as username in Woocommerce

I didn’t want my customers to remember a separate username so I thought why not automatically use their phone numbers as their username. They won’t have to remember their usernames this way.

If the registration form was built with GravityForms or custom form it was easy to map phone number as username but in the case of Woocommerce, we don’t have this control.

So I start digging the Woocommerce’s code and saw this filter of my interest `woocommerce_new_customer_data`. I started fiddling with it and came up with this code:

<?php
add_filter("woocommerce_new_customer_data" , "use_phone_as_username" , 10 ,1);
function use_phone_as_username($data ) {
	
	if(isset($_POST["billing_phone"])) {
		if(!username_exists($_POST["billing_phone"])) {
			$data["user_login"] = $_POST["billing_phone"];
		}
	}
	return $data;
}

You just need to copy this code to your functions.php and you should be good to go.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.