向后端的 woocommerce 订单表添加列

问题描述 投票:0回答:1

我想将我的字段添加为后端所有订单表中的新列。我已尝试以下操作,但未显示。有人知道这件事吗?请帮忙。

// 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';
        }
    }
}
php wordpress woocommerce orders user-data
1个回答
0
投票

使用以下代码版本,该版本也适用于高性能订单存储 (HPOS),并且如果贡献者 ID 已作为元数据记录在您的 WooCommerce 订单上,则应显示“贡献者”显示名称。

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;
}

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

代码位于活动子主题(或活动主题)的functions.php 文件中。应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.