我想添加一列来显示WooCommerce订单上的客户角色,我搜索和我找到的所有内容都是针对一个用户的。我也在这个链接上找到了这个代码(WooCommerce显示自定义栏),但我不明白我把我需要的东西放在哪里。我还发现了这个代码(https://gist.github.com/corsonr/5975207)但我没能获得用户角色。我加入了$user_role = $user->roles;
和
switch ($column)
{
case "user_role":
echo $user_role;
break;
}
不工作,我知道这是数组,但使用[0]或[1]不工作。
我错过了什么?我能做什么就做什么吗?
在您的主题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;
}
}
}