如何在 Woocommerce Admin 订单列表自定义列中显示用户名? [英] How to display usernames in Woocommerce Admin orders list custom column?

查看:76
本文介绍了如何在 Woocommerce Admin 订单列表自定义列中显示用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个显示 WordPress 用户的自定义 Woocommerce 订单概览列.

I'm trying to make a custom Woocommerce order overview column which displays the WordPress user.

这是位于 functions.php 文件中的代码:

This is the code which resides in the functions.php file:

// ADDING A NEW COLUMN
add_filter( 'manage_edit-shop_order_columns', 'k2i_extra_column_eldest_players', 20 );
function k2i_extra_column_eldest_players($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['skb-client'] = __( 'Oudste Speler','theme_domain');
        }
    }
    return $reordered_columns;
}

// Adding custom fields meta data for the column
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    switch ( $column )
    {
        case 'skb-client' :
            // Get custom post meta data
            $my_var_one = get_post_meta( $post_id, 'user_name', true );
            if(!empty($my_var_one))
                echo $my_var_one;

            // Testing (to be removed) - Empty value case
            else
                echo '<small>(<em>no value</em>)</small>';

            break;
    }
}

我认为我的错误在于这行代码:

I think my mistake is in this line of code:

$my_var_one = get_post_meta( $post_id, 'user_name', true );

元键 'user_name' 可能不是正确的...但我似乎找不到正确的.

The meta key 'user_name' probably isn't the right one... But I can't seem to find the right one.

对此的任何帮助将不胜感激.

Any help on this will be appreciated.

推荐答案

更新: woocommerce 中获取用户 ID 的正确meta_key订单是 _customer_user,所以:

Updated: The right meta_key to get the user ID in woocommerce orders is _customer_user, so:

$user_id = get_post_meta( $post_id, '_customer_user', true );

或者你可以使用 $the_order 全局对象作为:

Or you can use $the_order global object as:

global $the_order;
$user_id = $the_order->get_customer_id();

在您可以从该用户 ID 中获取 WordPress 用户数据后:

After you can get the WordPress userdata from this user ID:

$user_data = get_userdata( $userid );

echo 'Username: ' . $user_data->user_login . "<br>";
echo 'User email: ' . $user_data->user_email . "<br>";
echo 'User roles: ' . implode(', ', $user_data->roles) . "<br>";
echo 'User ID: ' . $user_data->ID . "<br>";
echo 'User full name: ' . $user_data->last_name .  ", " . $user_data->first_name . "<br>";

所以在你的代码中:

// Adding a custom new column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'custom_column_eldest_players', 20 );
function custom_column_eldest_players($columns)
{
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['skb-client'] = __( 'Oudste Speler','theme_domain');
        }
    }
    return $reordered_columns;
}

// Adding custom fields meta data for the column
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
    if ( 'skb-client' != $column ) return;

    global $the_order;

    // Get the customer id
    $user_id = $the_order->get_customer_id();

    if( ! empty($user_id) && $user_id != 0) {
        $user_data = get_userdata( $user_id );
        echo $user_data->user_login; // The WordPress user name
    }
    // Testing (to be removed) - Empty value case
    else
        echo '<small>(<em>Guest</em>)</small>';
}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效

Code goes in function.php file of your active child theme (or active theme). Tested and works

这篇关于如何在 Woocommerce Admin 订单列表自定义列中显示用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆