cachobong
cachobong

Reputation: 41

Woocommerce using JWT Authentication and using API for customers

I would want to use the Woocommerce REST API using JWT as the authentication and I would want to use the API for customers.

The point of the inquiry is that I am building a mobile e-commerce application on iOS and Android and would want to use my existing Woocommerce for this.

Upvotes: 1

Views: 2991

Answers (1)

J0nh1dd3n
J0nh1dd3n

Reputation: 171

You can get your user inside a custom endpoint like this:

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

the code above will return all data from the user related to the JWT token used for the request, so be careful if you don't want to send any sensitive data.

you can retrieve data from the user with the woocommerce Rest api, https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-an-order, or with the plugin functions:

$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() ),
) );

hope it helps.

Upvotes: 1

Related Questions