WooCommerce 订单页面添加客户角色自定义列
WooCommerce orders page add customer role custom column
我想添加列以显示客户在 WooCommerce 订单中的角色,我搜索后发现所有内容都是针对一个用户的。我还在这个 link (WooCommerce show custom column) 上找到了这段代码,但我不明白我把我需要的东西放在哪里。
我也找到了这段代码(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;
}
}
}
我想添加列以显示客户在 WooCommerce 订单中的角色,我搜索后发现所有内容都是针对一个用户的。我还在这个 link (WooCommerce show custom column) 上找到了这段代码,但我不明白我把我需要的东西放在哪里。
我也找到了这段代码(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;
}
}
}