I am looking to display user roles when issuing a GET request e.g: https://yourdomain.com/wp-json/wc/v3/orders.
The end goal is to use the user roles as a unique identifier in order to separate orders within an ERP based on the user role.
Any ideas?
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'add_user_roles_field_to_order_object', 10, 3 );
function add_user_roles_field_to_order_object( $response, $order, $request ) {
if ( empty( $response->data ) ) {
return $response;
}
$user_id = $order->get_user_id();
if ( is_int( $user_id ) && $user_id > 0 ) {
$userdata = get_userdata( $user_id );
$user_roles = $userdata->roles;
} else {
$user_roles = array();
}
$response->data['user_roles'] = $user_roles;
return $response;
}