通过手机号登录Woocommerce

问题描述 投票:0回答:2

我正在使用Woocommerce建立一个网站,默认的用户名是用户的电子邮件地址,我想为用户提供通过电话号码登录的功能。我如何才能做到这一点?请大家帮忙

login woocommerce
2个回答
0
投票

这是一个有趣的问题。有几件事需要考虑。

  1. 用户是否已经输入了他们的电话号码 或者在他们购买时给了他们号码?
  2. 我们是否使用计费电话号码来登录他们的账户?

假设这两件事都是真的,你就可以钩入 authenticate 筛选,搜索有匹配的用户 billing_phone 元,并返回该用户的 实际 用户名。

<?php

///
//  Allow login via phone number
///

function vnmAdmin_emailLogin($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)) {
            $username = $matchingUsers[0]->user_login;
        }

    }

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

add_filter('authenticate', 'vnmAdmin_loginWithPhoneNumber', 20, 3);

?>

注意:这个方法并不是绝对稳健的。 这个方法并不是绝对稳健的,例如,它没有检查空格(无论是在 "登录 "号码还是检索到的用户元号中);而且它只是简单地假设它找到的第一个用户是正确的--不管出于什么原因,如果你有多个用户使用同一个电话号码,那么这个方法并不理想。不过,我已经测试过了,它是可行的。


0
投票

函数名称与@indextwo答案中add_filter回调中给出的名称不同。我想用电子邮件和电话号码进行验证。使用了@indextwo答案,得出了这个结果。

///
//  Allow login via phone number and email
///

function vnmAdmin_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', 'vnmAdmin_loginWithPhoneNumber', 20, 3);
© www.soinside.com 2019 - 2024. All rights reserved.