【问题标题】:How to add the shipping zone name to a new column in WooCommerce admin order list如何将运输区域名称添加到 WooCommerce 管理订单列表中的新列
【发布时间】:2021-01-04 01:21:09
【问题描述】:

我想在 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 />';

    }

}

【问题讨论】:

    标签: wordpress woocommerce backend shipping orders


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的快速回复!我将以上内容添加到我的子主题中的 functions.php 中。但是,还没有将 Shipping_Zone 添加到订单表中。我需要调整上述内容吗?谢谢
    • @user3332692 是的,尝试使用相同的代码并将 shipping_zone 替换为您的元密钥
    • 还要添加你在functions.php文件中添加了什么
    【解决方案2】:

    所以试试这个

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

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 2018-09-05
      • 2018-09-09
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 2021-04-15
      相关资源
      最近更新 更多