# 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.

- Type: PHP
- Runs: Admin only
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, customers, export, csv
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-export-customers-csv/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Export Customers to CSV" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
