How to show user role in woocommerce orders list?

[ad_1]

I have found some code to show user role in orders list in woocommerce admin but it only works for non-HPOS setups. I have HPOS enables so it doesn’t seem to work. Can anyone help me show this on the order page?

[ad_2]
1 Comment
  1. This is the code that I found but doesn’t work with HPOS:

    /**
    * Add custom column.
    *
    * @param array $columns Columns.
    * @return array
    */
    function set_custom_edit_shop_order_columns( $columns ) {
    $columns[‘user_role’] = __( ‘Role’, ‘your-text-domain’ );
    return $columns;
    }
    add_filter( ‘manage_shop_order_posts_columns’, ‘set_custom_edit_shop_order_columns’, 100, 1 );

    /**
    * Add data to custom column.
    *
    * @param string $column Column slug.
    * @param id $post_id Post ID.
    */
    function custom_shop_order_column( $column, $post_id ) {
    if ( ‘user_role’ === $column ) {
    $order_user = get_post_meta( $post_id, ‘_customer_user’, true );
    if ( ! empty( $order_user ) ) {
    $user = get_user_by( ‘ID’, $order_user );
    if ( ! empty( $user ) ) {
    echo implode( ‘, ‘, ( array ) $user->roles );
    }
    }
    }
    }
    add_action( ‘manage_shop_order_posts_custom_column’, ‘custom_shop_order_column’, 100, 2 );

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer