Display user data in a new column on WooCommerce admin order list (+ HPOS)

45 views Asked by At

I want to add my field as new column in all orders table in backend. I have tried the following but its not displaying. Anyone know about this? Please help.

// Add custom column to orders list table
add_filter('manage_edit-shop_order_columns', 'custom_order_columns');
function custom_order_columns($columns)
{
    // Inserting the new column after 'order_status' column
    $new_columns = array();
    foreach ($columns as $column_name => $column_info) {
        $new_columns[$column_name] = $column_info;
        if ('order_status' === $column_name) {
            $new_columns['contributor'] = __('Contributor', 'textdomain');
        }
    }
    return $new_columns;
}

// Populate custom column with contributor information
add_action('manage_shop_order_posts_custom_column', 'custom_order_column_content', 10, 2);
function custom_order_column_content($column, $post_id)
{
    if ($column === 'contributor') {
        $contributor_id = get_post_meta($post_id, 'custom_order_contributor', true);
        if ($contributor_id) {
            $contributor = get_userdata($contributor_id);
            if ($contributor) {
                echo $contributor->display_name;
            } else {
                echo 'Not Assigned';
            }
        } else {
            echo 'Not Assigned';
        }
    }
}
1

There are 1 answers

0
LoicTheAztec On

Use the following code version that also works with Legacy orders and High-Performance Order Storage (HPOS), displaying the "contributor" display name, if the contributor ID has been recorded as metadata on your WooCommerce orders.

// Add a new column to admin orders list
add_filter( 'manage_woocommerce_page_wc-orders_columns', 'add_admin_order_list_custom_column' );
add_filter( 'manage_edit-shop_order_columns', 'add_admin_order_list_custom_column' );
function add_admin_order_list_custom_column( $columns ) {
    $reordered_columns = array(); // Initializing

    // 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['contributor'] = __('Contributor', 'textdomain');
        }
    }
    return $reordered_columns;
}

// Display new column data on admin orders list
add_action('manage_woocommerce_page_wc-orders_custom_column', 'display_admin_order_list_custom_column_content', 10, 2);
add_action('manage_shop_order_posts_custom_column', 'display_admin_order_list_custom_column_content', 10, 2);
function display_admin_order_list_custom_column_content( $column, $order ){
     if ( $column === 'contributor' ) {
        if( ! is_a($order, 'WC_order') && $order > 0 ) {
            $order = wc_get_order( $order );
        }
        // Get custom order metadata
        $contributor_id = (int) $order->get_meta('custom_order_contributor'); // Get contributor ID from order meta data
        $contributor    = get_userdata($contributor_id); // Get contributor WP_User object
        
        if ( $contributor && $contributor->display_name ) {
            echo $contributor->display_name;
        } else {
            _e('Not assigned', 'textdomain');
        }
    }
}

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