3

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?

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
user3123718
  • 31
  • 1
  • 3

1 Answers1

6

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.

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • 1
    I fixed the error using: include_once( ABSPATH . '/wp-content/plugins/woocommerce/includes/admin/reports/class-wc-report-customer-list.php' ); – Sabrina B. Jul 10 '18 at 20:27
  • Had to add a custom stock report... this worked perfectly. Top, top work! Thank you – Sam Holguin Sep 19 '18 at 20:52
  • I need to add more columns to the WooCommerce > under the WooCommerce tab called Customer List. I want to add columns phone number (billing phone number) and remove any fields if i want. I've saw this post but the solution mentioned is not working. – Mohsen Jun 29 '23 at 08:08