Php WooCommerce订单页面添加客户角色自定义列

Php WooCommerce订单页面添加客户角色自定义列,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我想添加列来显示WooCommerce订单上的客户角色,我搜索到的所有内容都是针对一个用户的。我也在这个链接()上找到了这段代码,但我不明白我把需要的东西放在哪里了。 我还发现了这个代码() 但我没有设法获得用户角色。 我添加了$user\u role=$user->roles 及 但它不起作用,我知道它是数组,但使用[0]或[1]不起作用 我错过了什么? 可以做什么?我做什么?在theme functions.php中添加以下代码 add_filter('manage_edit-shop_or

我想添加列来显示WooCommerce订单上的客户角色,我搜索到的所有内容都是针对一个用户的。我也在这个链接()上找到了这段代码,但我不明白我把需要的东西放在哪里了。 我还发现了这个代码() 但我没有设法获得用户角色。 我添加了
$user\u role=$user->roles

但它不起作用,我知道它是数组,但使用[0]或[1]不起作用

我错过了什么?
可以做什么?我做什么?

在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;
        }
    }

}

非常感谢。您知道是否可以添加按用户角色筛选,如按日期筛选?(我最好为此开始新的话题吗?)
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;
        }
    }

}