user3123718
user3123718

Reputation: 31

Add columns to WooCommerce Reports / Customer List

I need to add more columns to the WooCommerce > Reports under the Customer tab called Customer List.
I want to add columns address (billing_address_1), building number (billing_billing_number), city (billing_city), state(billing_state) and a custom field in my form called Apartment Complex (apt_complex).
How can I do this?

Upvotes: 3

Views: 4661

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

this is kind of near hard. But you can do it this way. This is the closest I can get.

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 {

        /**
         * Get column value.
         *
         * @param WP_User $user
         * @param string $column_name
         * @return string
         */
        public function column_default( $user, $column_name ) {
            global $wpdb;

            switch ( $column_name ) {

                case 'city' :
                    return get_user_meta( $user->ID, 'billing_city', true );
            }
            return parent::column_default( $user, $column_name );
        }

        /**
         * Get columns.
         *
         * @return array
         */
        public function get_columns() {

            /* default columns.
            $columns = array(
                'customer_name'   => __( 'Name (Last, First)', 'woocommerce' ),
                'username'        => __( 'Username', 'woocommerce' ),
                'email'           => __( 'Email', 'woocommerce' ),
                'location'        => __( 'Location', 'woocommerce' ),
                'orders'          => __( 'Orders', 'woocommerce' ),
                'spent'           => __( 'Money spent', 'woocommerce' ),
                'last_order'      => __( 'Last order', 'woocommerce' ),
                'user_actions'    => __( 'Actions', 'woocommerce' ),
            ); */

            // sample adding City next to Location.
            $columns = array(
                'customer_name'   => __( 'Name (Last, First)', 'woocommerce' ),
                'username'        => __( 'Username', 'woocommerce' ),
                'email'           => __( 'Email', 'woocommerce' ),
                'location'        => __( 'Location', 'woocommerce' ),
                'city'            => __( 'City', 'woocommerce' ),
            );
            return array_merge( $columns, parent::get_columns() );
        }
    }
}

I added City for you as an example. You can do the others you needed. It will look something like this:

reigelgallarde.me

As you can see, the City column has been added.

Upvotes: 6

Related Questions