This is sort of a follow-up question to this question that was successfully answered a while ago in WooCommerce orders page add customer role custom column answer code. I'm trying to see if it's possible to add some custom php to my site to allow the customer role to be seen on the individual orders page. Would there be a way to edit the code below to also show that data on the individual order page as well? Ideally I'd like this role to be only visible to admins and not the users themselves.
Jese Leos
August 10, 2024
Verified user
The following code can display user roles in the edit order page. function get_user_role_name( $user_id ) { // Get the user object $user = new WP_User( $user_id ); // Get the user's roles $roles = $user->roles; // Get the global WordPress roles object global $wp_roles; // Initialize an array to hold role names $role_names = array(); // Loop through the user's roles and get the display names foreach ( $roles as $role ) { if ( isset( $wp_roles->roles[$role] ) ) { $role_names[] = $wp_roles->roles[$role]['name']; } } // Return the role names return $role_names; } add_action( 'woocommerce_admin_order_data_after_payment_info', 'gs_woocommerce_admin_order_data_after_payment_info' ); function gs_woocommerce_admin_order_data_after_payment_info($order){ if ( $order->get_user_id() == 0 ) { echo '<b>This order was placed by a guest.</b>'; } else { $role_names = get_user_role_name( $order->get_user_id() ); // Display the role names if ( !empty( $role_names ) ) { echo '<b>User Roles:</b> ' . implode( ', ', $role_names ); } } } We are using woocommerce_admin_order_data_after_payment_info action hook, so, the user roles will display as show in the screenshot before.