skip to Main Content

I want to show the shipping zone name on order overview in WooCommerce.

This shows the order overview

enter image description here


This shows the shipping zone name I want to display

enter image description here


I have read that I can either do it with my own plugin, tried and failed or using a filter in my functions.

I found this filter, to add date to the same orders screen.

How can I adjust this to show shipping zone?


My code so far:

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

    }

}

2

Answers


  1. Try this filter and action to add custom column in admin panel

    // 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;
       }
    }
    
    Login or Signup to reply.
  2. So try this instead

    // 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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search