Support » Plugin: WooCommerce » Wc-reports – customer_list: columns sortable

  • Resolved crslz

    (@crslz)


    Hi,

    I’m trying to make the ‘orders’ and ‘money spent’ columns sortable under the woocommerce reports.

    wc-reports&tab=customers&report=customer_list

    Now I have this

    create a filter to woocommerce_admin_reports. Specifically, we need to change the callback of the customer list reports. Below it’s ‘customer_list_get_report’.

    add_filter( 'woocommerce_admin_reports', 'woocommerce_admin_reports' );
    function woocommerce_admin_reports( $reports ) {
    
        $reports['customers']['reports']['customer_list']['callback'] = 'customer_list_get_report';
    
        return $reports;
    }

    then create the function ‘customer_list_get_report’. This function generates the reports. Take note of the do_action, this is where we include the class WC_Report_Customer_List for us to be able to extend to it and overwrite some of it’s functions.

    function customer_list_get_report( $name ) {
    
        $class = 'My_WC_Report_Customer_List';
    
        do_action('class_wc_report_customer_list');
    
        if ( ! class_exists( $class ) )
            return;
    
        $report = new $class();
        $report->output_report();
    }

    This, below, is where you make your edits.

    add_action( 'class_wc_report_customer_list', 'class_wc_report_customer_list' );
    function class_wc_report_customer_list() {
    
        if ( ! class_exists( 'WC_Report_Customer_List' ) ) {
            include_once( WC_ABSPATH . 'includes/admin/reports/class-wc-report-customer-list.php' );
        }
        class My_WC_Report_Customer_List extends WC_Report_Customer_List {
            private function sort_data( $a, $b ) {
                // Set defaults
                $orderby = 'title';
                $order = 'asc';
                // If orderby is set, use this as the sort column
                if(!empty($_GET['orderby'])) {
                    $orderby = $_GET['orderby'];
                }
                // If order is set use this as the order
                if(!empty($_GET['order'])) {
                    $order = $_GET['order'];
                }
                $result = strcmp( $a[$orderby], $b[$orderby] );
                if($order === 'asc') {
                    return $result;
                }
                return -$result;
            }
        }
    }

    thanks for any help!

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.