在Woocommerce管理员订单列表自定义列中显示用户名

时间:2018-04-12 18:26:49

标签: php wordpress woocommerce username orders

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

这是驻留在functions.php文件中的代码:

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

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

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

元键'user_name'可能不是正确的...但我似乎无法找到合适的元素。

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

已更新:获取woocommerce订单中用户ID 的权限meta_key_customer_user,因此:

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

或者您可以将$the_order全局对象用作:

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

从此用户ID获取WordPress用户数据后:

$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文件中。经过测试和工作

enter image description here