I followed this post to add the shipping zone name of an order to the WC orders view. ADD SHIPPING ZONE NAME TO WC ORDERS COLUMN It works great. However, I need to be able to sort the orders by delivery zone.
This is the code snippet I used to make the columns sortable but it doesn't work. Any help would be appreciated. Thank you.
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns )
{
$custom = array(
'shipping_zone' => 'current_zone_name',
);
return wp_parse_args( $custom, $columns );
}
Here is the full code I used:
// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
// Add new column
$columns['shipping_zone'] = 'Delivery 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 );
add_filter( "manage_edit-shop_order_sortable_columns", 'shop_order_column_meta_field_sortable' );
function shop_order_column_meta_field_sortable( $columns )
{
$meta_key = 'current_zone_name';
return wp_parse_args( array('shipping_zone' => $meta_key), $columns );
}