Woocommerce通过电话号码登录

时间:2015-04-26 18:15:19

标签: login woocommerce

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

2 个答案:

答案 0 :(得分:0)

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

  1. 用户是否已经在他们的帐户中实际输入了他们的电话号码,或者在他们购买时给出了他们的号码?
  2. 我们是否使用结算电话号码登录?
  3. 假设这两件事情都属实,您可以访问authenticate过滤器,搜索匹配billing_phone元的用户,并返回该用户的实际用户名:< / p>

    <?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);
    
    ?>
    

    注意:此方法并非绝对健壮;例如,它不检查空格(在“登录”号码或检索到的用户元数字中);并且它只是假设它找到的第一个用户是正确的 - 如果由于某种原因,你有多个用户使用相同的电话号码,这是不理想的。但话说回来,我已经对它进行了测试并确实有效。

答案 1 :(得分: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);