sort – Woocommerce Admin Order sorting not working

Question

I have created a custom column called “Volume” to count the total number of completed order for each order. Column added successfully, value also being fetched without any issue. I registered the column as sortable and added an action to sort the column, but it is not working. The column to be sorted is in numeric. Below is my code

   add_filter('manage_edit-shop_order_columns', 'misha_order_items_column_volume' );
function misha_order_items_column_volume( $order_columns ) {
    $order_columns['order_products_volume'] = "Volume";
    return $order_columns;
}

add_action( 'manage_shop_order_posts_custom_column' , 'misha_order_items_column_cnt_volume' );
function misha_order_items_column_cnt_volume( $colname ) {
    global $the_order; // the global order object
    if( $colname == 'order_products_volume' ) {
        $order_items = $the_order->get_id();
        $order = new WC_Order( $order_items );
        $first_name=$order->get_billing_first_name();
        $last_name=$order->get_billing_last_name();
        $args = array(
            'billing_first_name' => $first_name,
            'billing_last_name' => $last_name,
            'post_status' => 'completed',
            'post_type' => 'shop_order',
            'return' => 'ids',
            'numberposts' => -1
        );  
        echo intval(count(wc_get_orders( $args )));
    }
}


function register_sortable_columns( $columns ) {
    $order_columns['order_products_volume'] = array('order_products_volume','true');
    return $order_columns;
}
add_filter( 'manage_edit-shop_order_sortable_columns', 'register_sortable_columns' ); 

//This is not working
add_action( 'pre_get_posts', 'closing_column_orderby' );  
function closing_column_orderby( $query ) {  
    if( ! is_admin() )  
        return;  

    $orderby = $query->get( 'orderby');  
    if( 'order_products_volume' == $orderby ) {  
        $query->set('meta_key','order_products_volume'); 
        $query->set('meta_type','NUMBER');
        $query->set('orderby','meta_value_num');  
    }  
} 

0
KP Developers 1 month 2022-07-08T12:02:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse