Lucas Craven
Lucas Craven

Reputation: 98

WooCommerce Orders: Is it possible to add another column and populate it with custom meta fields from Users?

So short version is that I am trying to add an extra column in the orders page and have it populated with the custom field I have created for the users on the site (The name for the custom field is account_manager)

What I'm trying to achieve is for it display the customers account manager in a column in the orders section. Bit of a strange request I know!

I had a look at a few guides and tutorials regarding columns and meta data but it all seemed to be regarding the orders rather than data from the customer directly.

Any help would be greatly appreciated. I don't mind having a go, I just need a little guidance :)

I've tried this but it's not returning any values

function wc_orders_add_account_manager_column($columns)
{
    $new_columns = [];
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'order_status' === $column_name ) {    // Change order_status to manage column orders
            $new_columns['account_manager'] = 'Account Manager';
        }
    }
    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_orders_add_account_manager_column', 20 );


/**
 * Adds 'account_manager' column content to 'Orders' page
 *
 * @param string $column name of column being displayed
 */
function wc_orders_add_account_manager_column_content($column)
{
   global $post;
    $order_id = $post->ID;

    // Get an instance of the WC_Order object
    $order = wc_get_order($order_id);

    // Get the user ID from WC_Order methods
    $user_id = $order->get_customer_id(); // or $order->get_customer_id();
    $meta = get_user_meta($user_id, 'account_manager', true);

    return $meta;
    if ( 'account_manager' === $columns ) {
        echo $meta ;
    } else {
        echo "Not Valid!";
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_orders_add_account_manager_column_content' );

Upvotes: 0

Views: 659

Answers (1)

Yashar
Yashar

Reputation: 651

I'm assuming you have stored this 'account_manager' as an order meta on postmeta table. You need to use manage_edit-shop_order_columns filter hook to add the new column to admin orders page and manage_shop_order_posts_custom_column action hook to populate this column.

/**
 * Adds 'account_manager' column header to 'Orders' page immediately after 'Order Status' and before 'Total' column.
 *
 * @param array $columns
 * @return array
 */
function wc_orders_add_account_manager_column($columns)
{
    $new_columns = [];
    foreach ( $columns as $column_name => $column_info ) {
        $new_columns[ $column_name ] = $column_info;
        if ( 'order_status' === $column_name ) {    // Change order_status to manage column orders
            $new_columns['account_manager'] = 'Account Manager';
        }
    }
    return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'wc_orders_add_account_manager_column', 20 );


/**
 * Adds 'account_manager' column content to 'Orders' page
 *
 * @param string $column name of column being displayed
 */
function wc_orders_add_account_manager_column_content($column)
{
    global $post;
    $order      = wc_get_order( $post->ID );
    $user_id    = $order->get_customer_id(); // or $order->get_customer_id();
    $meta       = get_user_meta($user_id, 'account_manager', true);
    if ( 'account_manager' === $column ) {
        echo $meta ? $meta : 'Not Valid!';
    }
}
add_action( 'manage_shop_order_posts_custom_column', 'wc_orders_add_account_manager_column_content' );

Tested & It's Working

Note: As I mentioned in the code comments, you can change the order of the columns by changing order_status in this code block:

if ( 'order_status' === $column_name ) {
    $new_columns['account_manager'] = 'Account Manager';
}

In this case, our new account_manager column will display after the order_status column

Upvotes: 1

Related Questions