How to Add WooCommerce Login With Phone Number

Add WooCommerce Login With Phone NumberAre you looking for a way to allows users in your WooCommerce store to log in with their phone numbers? This post aims to provide you with a solution for your site.

To improve the user experience on your site, you may want to allow shoppers to log into their accounts with their phone numbers. However, by default, WooCommerce allows users to log in with their username and a password.

It is worth mentioning that you need some coding skills before you proceed. You should also create a child theme. This will ensure that your changes are not lost during an update.

But it is important to note that this tutorial does not intend to show login with OTP.

Add WooCommerce Login With Phone Number

In this tutorial, we will add one more option of phone number along with username and email. Users will use any option, which is convenient for them. We will achieve this by using a custom PHP script.

Let us get right into it.

Steps to Add WooCommerce Login With Phone Number

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will add WooCommerce login with phone number.
  3. Add the following code to the php file:
[php] ///

//  Allow login via phone number and email

///

function njengah_loginWithPhoneNumber($user, $username, $password) {

    //  Try logging in via their billing phone number

    if (is_numeric($username)) {

        //  The passed username is numeric – that’s a start

        //  Now let’s grab all matching users with the same phone number:

        $matchingUsers = get_users(array(

            ‘meta_key’     => ‘billing_phone’,

            ‘meta_value’   => $username,

            ‘meta_compare’ => ‘LIKE’

        ));

        //  Let’s save time and assume there’s only one.

        if (is_array($matchingUsers) && !empty($matchingUsers)) {

            $username = $matchingUsers[0]->user_login;

        }

    }elseif (is_email($username)) {

        //  The passed username is email- that’s a start

        //  Now let’s grab all matching users with the same email:

        $matchingUsers = get_user_by_email($username);

        //  Let’s save time and assume there’s only one.

        if (isset($matchingUsers->user_login)) {

            $username = $matchingUsers->user_login;

        }

    }

    return wp_authenticate_username_password(null, $username, $password);

}

add_filter(‘authenticate’, ‘njengah_loginWithPhoneNumber’, 20, 3);
[/php]

  1. This will allow users to log in using their phone number, username, or email. However, there are a couple of things to consider. The user needs to have actually entered their phone number against their account already.

Conclusion

That is all you need to do to allow customers in your store to log in with a phone number. This makes it very easy for users to log into your store.

If you need additional functionality on the login page, you can use a plugin or consult a qualified WordPress developer.

We hope that this tutorial helped to solve your problem.

Similar Articles

  1. How to Add WooCommerce Products to Facebook Shop