Skip to content
Advertisement

WooCommerce orders page add customer role custom column

I want to add column to show the customer role on the WooCommerce orders, I search and everything I found is for one user. I also found this code on this link (WooCommerce show custom column) but I do not understand where I put what I need. I also found this code (https://gist.github.com/corsonr/5975207) but I do not managed to get the user role. I added $user_role = $user->roles; and

switch ($column)
    {
        case "user_role":
            echo $user_role;
        break;  

    }

but it not worked, I know it is array but use of [0] or [1] did not work.

I missing something? It is possible to do what I what?

Advertisement

Answer

Add below code in your theme functions.php

add_filter('manage_edit-shop_order_columns', 'add_column_heading', 20, 1);

function add_column_heading($array) {


    $res = array_slice($array, 0, 2, true) +
            array("customer_role" => "Customer Role") +
            array_slice($array, 2, count($array) - 1, true);

    return $res;
}

add_action('manage_posts_custom_column', 'add_column_data', 20, 2);

function add_column_data($column_key, $order_id) {

    // exit early if this is not the column we want
    if ('customer_role' != $column_key) {
        return;
    }

    $customer = new WC_Order( $order_id );
    if($customer->user_id != ''){
            $user = new WP_User( $customer->user_id );
             if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
            foreach ( $user->roles as $role )
               echo $role;
        }
    }

}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement