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

问题描述

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

这显示了订单概述

enter image description here


这显示了我要显示的运输区域名称

enter image description here


我已经读到我可以使用自己的插件来尝试或失败,也可以在函数中使用过滤器。

我找到了此过滤器,以将日期添加到同一订单屏幕。

如何调整显示运费区域?


到目前为止,我的代码:

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

    }

}

解决方法

所以试试这个

// 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',2 );
,

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

// 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",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;
   }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...