donglijuan8227 2019-06-22 17:32
浏览 64
已采纳

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

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' );
  • 写回答

1条回答 默认 最新

  • doukuipei9938 2019-06-22 21:05
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能