首页 文章

Woocommerce登录Custome API endpoints

提问于
浏览
0

我正在使用woocommerece商店开发应用程序并使用Woocommerce REST API获取产品和订单详细信息,但现在我在登录时面临问题,因为Woocommerce没有提供这种类型的API . 所以我正在创建一个自定义 endpoints 并尝试这个,但我收到错误404,没有可用的休息路由 .

这是我注册的自定义终点 .

add_action( 'rest_api_init', function () {
 register_rest_route( 'wc/v2', '/login/)', array(
 'methods' => 'POST',
 'callback'=> 'my_awesome_func',
 'args' => array(
 ), 
 ) );
} );

这是登录的登录,但我想我在某个地方做错了所以请检查并帮助我 .

function my_awesome_func( WP_REST_Request $request ) {
global $wpdb;
$username = $request['email'];
$password = $request['password'];
$db = new DbOperation();
    $response = array();    
    $login_data = array();
    $login_data['user_login'] = $username;
    $login_data['user_password'] = $password;
    $results = $wpdb->get_row( "SELECT ID FROM rd_users WHERE user_email='".$username."'");
    $activation_id = $results->ID;
    $activation_key =  get_user_meta( $activation_id, 'has_to_be_activated', true );
    if($activation_key != false ){
       $results = 2;//if activation key exists than show the error
    }
     else{
            $user_verify = wp_signon( $login_data, false ); 
            if ( is_wp_error($user_verify) ) 
            {
              $results = 0; //show invalid username and password.
            }
             else {    
               $results = 1; //login success.
            }
    }
    if ($results== 1) {
    $user_info = get_userdata($student[0]->ID);
    $response['id'] = $user_info->ID;
    $response['name'] = $user_info->display_name;
    $response['fname'] = $user_info->first_name;
    $response['lname'] = $user_info->last_name;
    $response['email'] = $user_info->user_email;
    $response['status'] = 1;
    $response["error"] = false;
    $response['message'] = "You have successfully Logedin!";
} else {
    if($results == 0){
    $response['status'] = 0;
    $response["error"] = true;
    $response['message'] = "Invalid username or password";
    }
    else{
        $response['status'] = 2;
        $response["error"] = true;
        $response['message'] ="Your account has not been activated yet.
         To activate it check your email and clik on the activation link.";
        }
}
return  $response;

}

1 回答

  • 0

    看看我找到了什么,使用了https身份验证 . 在邮递员中,不使用oAuth1.0作为身份验证,而是使用基本身份验证并将使用者密钥作为用户名传递 . 密码应该是消费者的秘密 .

    我希望这会奏效 .

相关问题