如何将运输区域名称添加到 WooCommerce 管理订单列表中的新列

How to add the shipping zone name to a new column in WooCommerce admin order list

我想在 WooCommerce 的订单概览中显示送货区域名称。

这显示了订单概览


这里是我要显示的配送区域名称


我了解到我可以使用我自己的插件来完成,尝试过但失败了,或者在我的函数中使用过滤器。

我找到了这个过滤器,用于将日期添加到同一订单屏幕。

如何调整它以显示送货区域?


到目前为止我的代码:

add_action( 'manage_posts_custom_column', 'misha_date_clmn' );
function misha_date_clmn( $column_name ) {
    global $post;
    if( $column_name  == 'order_date' ) {

        echo strtotime( $post->post_date ) . '<br />';

    }

}

尝试使用此过滤器和操作在管理面板中添加自定义列

// Filter to add custom column to custom post types
add_filter('manage_shop_order_posts_columns', "edit_shop_order_columns");

function edit_shop_order_columns($columns) {
    $columns['shipping_zone'] = "Shipping Zone";
    return $columns;
}

// Action to display data in column
add_action('manage_shop_order_posts_custom_column', "edit_shop_order_columns_data", 10, 2);
function edit_shop_order_columns_data($column, $post_id) {
    $shipping_zone = get_post_meta($post_id, "shipping_zone", true); // Use your logic here to value of shipping zone
    switch ($column) {
        case 'shipping_zone' :
            echo !empty($shipping_zone) ? $shipping_zone : "";
            break;
   }
}

所以试试这个

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['shipping_zone'] = 'Shipping zone';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'shipping_zone' ) {
        // Get order
        $order = wc_get_order( $post_id );

        // Iterating through order shipping items
        foreach( $order->get_items( 'shipping' ) as $item_id => $shipping_item_obj ) {
            $shipping_method_instance_id = $shipping_item_obj->get_instance_id(); // The instance ID
        }
        
        // Get zone by instance id
        $shipping_zone = WC_Shipping_Zones::get_zone_by( 'instance_id', $shipping_method_instance_id );
        
        // Get zone name
        $current_zone_name = $shipping_zone->get_zone_name();
        
        if ( ! empty ( $current_zone_name ) ) {
            echo $current_zone_name;    
        }
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );