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

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

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失