WooCommerce 订单:是否可以添加另一列并使用来自用户的自定义元字段填充它?

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

如此简短的版本是我试图在订单页面中添加一个额外的列,并用我为网站上的用户创建的自定义字段填充它(自定义字段的名称是 account_manager)

我想要实现的是它在订单部分的一列中显示客户客户经理。我知道有点奇怪的要求!

我查看了一些关于列和元数据的指南和教程,但它们似乎都与订单有关,而不是直接来自客户的数据。

如有任何帮助,我们将不胜感激。我不介意试一试,我只需要一点指导:)

我试过了,但没有返回任何值

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' );

我假设您已将此 'account_manager' 作为订单元存储在 postmeta table 上。您需要使用 manage_edit-shop_order_columns 过滤器挂钩将新列添加到管理订单页面,并使用 manage_shop_order_posts_custom_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' );

已测试且有效

注意: 正如我在代码注释中提到的,您可以通过更改此代码块中的 order_status 来更改列的顺序:

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

在这种情况下,我们新的 account_manager 列将显示在 order_status 列之后