Search code examples
phpwordpresswoocommercehook-woocommerceorders

Display user bio in a column on Woocommerce admin orders list


We store some customer info in his/her Bio text area in user edit page. How to display this stored data in user Bio field as a column in the Order admin page in woocommerce ? I tried this code but its not working:

// 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 Bio
    $user_bio = get_user_meta( $order->get_customer_id(), 'description', true );


        $user_data = get_userdata( $user_bio );
        echo $user_data->user_bio; // The WordPress user name

}

we need this stored data to displayed for each costumers purchase. Best regards


Solution

  • The variable $order is not defined in your 2nd function, so you need to use instead $the_order. then I have revisited you code… Try the following instead:

     // 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['user-bio'] = __( 'User bio', 'woocommerce');
            }
        }
        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 ( 'user-bio' === $column ) {
            global $the_order;
    
            echo ( $user = $the_order->get_user() ) ? $user->description : 'n/c';
        }
    }
    

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