Woocommerce 使用 JWT 身份验证并为客户使用 API

Woocommerce using JWT Authentication and using API for customers

我想使用 Woocommerce REST API,使用 JWT 作为身份验证,我想为客户使用 API。

询问的重点是我正在 iOS 和 Android 上构建一个移动电子商务应用程序,并且想为此使用我现有的 Woocommerce。

您可以像这样让您的用户进入自定义端点:

if (is_user_logged_in()) {
    $user = wp_get_current_user();
    if($user->exists()){
        return rest_ensure_response($user);

    }
}
return rest_ensure_response(array("data" => "no user logged in"));

上面的代码将 return 来自用户的与用于请求的 JWT 令牌相关的所有数据,所以如果您不想发送任何敏感数据,请务必小心。

您可以使用 woocommerce Rest api、https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-an-order 或插件功能从用户那里检索数据:

$query = new WC_Order_Query();
$query->set( 'customer', $user->data->user_email);
$orders = $query->get_orders();
$customer_orders = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => get_current_user_id(),
            'post_type'   => wc_get_order_types(),
            'post_status' => array_keys( wc_get_order_statuses() ),
) );

希望对您有所帮助。