Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Sample code to import customers in WooCommerce


Sample code to import customers in WooCommerce


Here's a sample PHP code snippet to import customers into WooCommerce programmatically:

php
function import_woocommerce_customers($customers_data) {
    foreach ($customers_data as $customer) {
        $user_id = wc_create_new_customer(
            $customer['email'], 
            $customer['username'], 
            $customer['password']
        );

        if (!is_wp_error($user_id)) {
            // Update customer meta data
            update_user_meta($user_id, 'first_name', $customer['first_name']);
            update_user_meta($user_id, 'last_name', $customer['last_name']);
            update_user_meta($user_id, 'billing_phone', $customer['phone']);
            
            // Update billing address
            update_user_meta($user_id, 'billing_address_1', $customer['billing_address']);
            update_user_meta($user_id, 'billing_city', $customer['billing_city']);
            update_user_meta($user_id, 'billing_state', $customer['billing_state']);
            update_user_meta($user_id, 'billing_postcode', $customer['billing_postcode']);
            update_user_meta($user_id, 'billing_country', $customer['billing_country']);
            
            // Set user role to 'customer'
            $user = new WP_User($user_id);
            $user->set_role('customer');
            
            echo "Customer imported successfully: " . $customer['email'] . "\n";
        } else {
            echo "Error importing customer: " . $customer['email'] . " - " . $user_id->get_error_message() . "\n";
        }
    }
}

// Sample customer data
$customers_data = array(
    array(
        'email' => '
 <script language='JavaScript' type='text/javascript'>
 <!--
 var prefix = 'm&#97;&#105;lt&#111;:';
 var suffix = '';
 var attribs = '';
 var path = 'hr' + 'ef' + '=';
 var addy8644 = 'c&#117;st&#111;m&#101;r1' + '&#64;';
 addy8644 = addy8644 + '&#101;x&#97;mpl&#101;' + '&#46;' + 'c&#111;m';
 document.write( '<a ' + path + '\'' + prefix + addy8644 + suffix + '\'' + attribs + '>' );
 document.write( addy8644 );
 document.write( '<\/a>' );
 //-->
 </script><script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '<span style=\'display: none;\'>' );
 //-->
 </script>This e-mail address is being protected from spambots. You need JavaScript enabled to view it
 <script language='JavaScript' type='text/javascript'>
 <!--
 document.write( '</' );
 document.write( 'span>' );
 //-->
 </script>',
        'username' => 'customer1',
        'password' => 'password123',
        'first_name' => 'John',
        'last_name' => 'Doe',
        'phone' => '1234567890',
        'billing_address' => '123 Main St',
        'billing_city' => 'New York',
        'billing_state' => 'NY',
        'billing_postcode' => '10001',
        'billing_country' => 'US'
    ),
    // Add more customer data as needed
);

// Call the import function
import_woocommerce_customers($customers_data);

This code demonstrates how to programmatically import customers into WooCommerce[1][2][4]. Here's a breakdown of what the code does:

1. It defines a function `import_woocommerce_customers` that takes an array of customer data as input.

2. For each customer in the array, it creates a new WooCommerce customer using `wc_create_new_customer()`, which is a WooCommerce function for creating customers[4].

3. If the customer is created successfully, it updates various user meta fields with the customer's information, including name, phone, and billing address details[1][2].

4. It sets the user role to 'customer' using WordPress's `WP_User` class.

5. The function then outputs a success or error message for each customer import attempt.

6. At the end, there's a sample array of customer data and a call to the import function to demonstrate its usage.

To use this code:

1. Ensure you have WooCommerce installed and activated on your WordPress site.
2. Modify the `$customers_data` array to include your actual customer data.
3. Run this code in a PHP environment with access to WordPress and WooCommerce functions (e.g., in a custom plugin or theme function).

Remember to handle any errors appropriately and consider performance implications for large imports. For large-scale imports, you might want to use batch processing or a more robust import solution[1][3][5].

Citations:
[1] https://www.wpallimport.com/documentation/how-to-import-woocommerce-customers/
[2] https://woocommerce.com/document/import-and-export-of-woocommerce-users/
[3] https://www.webtoffee.com/best-way-import-customers-woocommerce/
[4] https://woocommerce.com/document/customer-order-csv-import-suite/
[5] https://usersinsights.com/woocommerce-customer-import/