Are 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:
- Log into your WordPress site and access the Dashboard as the admin user.
- 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.
- Add the following code to the php file:
// 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]
- 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
- How to Customize Product Category Page In WooCommerce
- How to Set Up WooCommerce You Must be Logged in to Checkout
- How to Send to Mpesa WooCommerce Payment for Free
- WooCommerce Redirect Users After Registration by Roles
- How to Create WooCommerce Redirect After Checkout Per Product
- 5 Best WordPress Pagination Plugins for Better Navigation
- How to Add Checkout Phone Number Validation WooCommerce
- How to Add Wishlist In WooCommerce
- WooCommerce Logout without Confirmation : How to Remove “Are you sure you want to log out?”
- How to Create WooCommerce Login Logout Shortcode
- 30+ Best Comment Plugin for WordPress to Boost Engagement
- How to Add Google Analytics to WooCommerce
- How to Add Social Login In WooCommerce Social Login
- How to Display All Products Purchased by User – Purchase History
- How to Redirect Users after Successful Login in WordPress without Using a Plugin
- How to Redirect On Refresh WordPress Page » Detect Page Refresh PHP
- How to Add WooCommerce Products to Facebook Shop