我想添加一栏以显示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;
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句