admin管理员组

文章数量:1125605

I've created a class that extend WP_List_table to manage WC orders in a plugin admin menu.

public static function get_orders( $per_page = 5, $page_number = 1 ) {

    global $wpdb;
    
    $query = new WC_Order_Query( array(
        'limit' => $per_page,
    ) );
    
    
    if ( ! empty( $_REQUEST['orderby'] ) ) {
        $order_by = (isset($_REQUEST['orderby']) && in_array($_REQUEST['orderby'], array_keys(self::get_sortable_columns()))) ? $_REQUEST['orderby'] : 'date';
        $query->set( 'orderby', $order_by );
    }
        
    //$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
        
    $orders = $query->get_orders();
    
    return $orders;
}

And the sortable columns definition :

public function get_sortable_columns() {
        $sortable_columns = array(
            'order_modified_date' => array( 'modfied', true ) ,
            'order_customer' => array( 'order_organization', true ) ,
            'order_organization' => array( 'order_organization', true ) ,
            'order_total' => array( 'order_total', false ) ,
            'date' => array( 'date', false )
        );
        return $sortable_columns;
    }

Also the prepare_items function as follow :

public function prepare_items() {

        $this->_column_headers = $this->get_column_info();

        /** Process bulk action */
        $this->process_bulk_action();
        
        $per_page     = $this->get_items_per_page( 'customers_per_page', 5 );
        $current_page = $this->get_pagenum();
        /** get data from get_orders **/
        $orders = self::get_orders( $per_page, $current_page );

        $data = array();
        foreach($orders as $order){
            $data[] = array(
               'ID' => $order->get_id() ,
                'date'  => $order->get_date_created()->date('Y-m-d'),
                'order_customer' => $order->get_user_id(),
                'order_modified_date' => $order->get_date_modified(),
                'order_organization' => 'null',
                'order_product' => 'get_order_product',
                'order_payment_method' => $order->get_payment_method_title(),
                'order_status' => $order->get_status() ,
                'order_total' => $order->get_total(),
            );
        }
        $total_items  = count( $data );

        $this->set_pagination_args( [
            'total_items' => $total_items, 
            'per_page'    => $per_page 
        ] );

        $this->items = $data;
    }

I tried several approach but my column 'date' sorting doesn't work at all.

Any ideas what I'm doing wrong?

Thanks

本文标签: plugin developmentWPListTable sortable column with WCOrderQuery doesn39t work