Halloween Sale 15% off Ends in See the plans
PHP

Export Customers to CSV

Adds a Customers CSV export under WooCommerce > Export customers with names, emails, phone, location, order count and total spent for every customer account.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'admin_menu', function () {
    add_submenu_page( 'woocommerce', 'Export customers', 'Export customers', 'manage_woocommerce', 'wpide-export-customers', function () {
        ?>
        <div class="wrap">
            <h1><?php echo esc_html( 'Export customers' ); ?></h1>
            <p><?php echo esc_html( 'Download every customer account as a CSV file you can open in Excel, Numbers or Google Sheets.' ); ?></p>
            <form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
                <input type="hidden" name="action" value="wpide_export_customers">
                <?php wp_nonce_field( 'wpide_export_customers' ); ?>
                <?php submit_button( 'Download CSV', 'primary', 'submit', false ); ?>
            </form>
        </div>
        <?php
    }, 60 );
} );

add_action( 'admin_post_wpide_export_customers', function () {
    check_admin_referer( 'wpide_export_customers' );

    if ( ! current_user_can( 'manage_woocommerce' ) ) {
        wp_die( esc_html( 'You are not allowed to export customers.' ), 403 );
    }

    $cell = function ( $value ) {
        $value = (string) $value;

        return preg_match( '/^[=+\-@\t\r]/', $value ) ? "'" . $value : $value;
    };

    nocache_headers();
    header( 'Content-Type: text/csv; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename=customers-' . gmdate( 'Y-m-d' ) . '.csv' );

    $out = fopen( 'php://output', 'w' );
    fwrite( $out, "\xEF\xBB\xBF" );
    fputcsv( $out, array( 'ID', 'First name', 'Last name', 'Email', 'Phone', 'City', 'Postcode', 'Country', 'Registered', 'Orders', 'Total spent' ) );

    $page = 1;

    do {
        $ids = get_users( array( 'role' => 'customer', 'fields' => 'ID', 'number' => 500, 'paged' => $page, 'orderby' => 'ID', 'count_total' => false ) );

        foreach ( $ids as $id ) {
            $customer = new WC_Customer( $id );

            fputcsv( $out, array_map( $cell, array(
                $id,
                $customer->get_first_name(),
                $customer->get_last_name(),
                $customer->get_email(),
                $customer->get_billing_phone(),
                $customer->get_billing_city(),
                $customer->get_billing_postcode(),
                $customer->get_billing_country(),
                $customer->get_date_created() ? $customer->get_date_created()->date( 'Y-m-d' ) : '',
                $customer->get_order_count(),
                wc_format_decimal( $customer->get_total_spent(), 2 ),
            ) ) );
        }

        $page++;
    } while ( count( $ids ) === 500 );

    fclose( $out );
    exit;
} );

Notes

Exports customer accounts, not guest orders, in batches of 500, and only store managers can run it.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.