16

Is there anyway to create a customer programmatically like you can with a WordPress user. Obviously the WooCommerce user shares some of the same WordPress user fields, there there is additional content that would need to be set like Billing / Postal address.

Has anyone achieved this before? I can't find anything in the WooCommerce API / functions list on their website.

EDIT: Just found this: http://docs.woothemes.com/wc-apidocs/function-wc_create_new_customer.html

But how can I then provide other field details (like addresses).

Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84

2 Answers2

39

WooCommerce customer is essentially a WordPress user with extra metadata. So once the user is created you can add metadata to it by using update_user_meta function. Navigate to Users -> All Users, edit one of the users and then scroll down to see the fields.

Made up code given below to give you the gist of how it works.

$user_id = wc_create_new_customer( $email, $username, $password );

update_user_meta( $user_id, "billing_first_name", 'God' );
update_user_meta( $user_id, "billing_last_name", 'Almighty' );
.... more fields

Here is the full list of billing and shipping fields

Billing

  • billing_first_name
  • billing_last_name
  • billing_company
  • billing_address_1
  • billing_address_2
  • billing_city
  • billing_postcode
  • billing_country
  • billing_state
  • billing_email
  • billing_phone

Shipping

  • shipping_first_name
  • shipping_last_name
  • shipping_company
  • shipping_address_1
  • shipping_address_2
  • shipping_city
  • shipping_postcode
  • shipping_country
  • shipping_state
Anand Shah
  • 14,575
  • 16
  • 72
  • 110
  • Perfect thanks. I came to the same conclusion over lunch but I'll mark your answer for future users who come across it. – Stefan Dunn Sep 24 '15 at 13:46
  • @AnandShah could you please reply with a link from where you got this list of fields? please do it asap. – Shahid Sarwar Aug 18 '16 at 10:16
  • 2
    https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ – Anand Shah Aug 19 '16 at 07:44
  • **New link** [update_user_meta](https://developer.wordpress.org/reference/functions/update_user_meta/) – Fil Feb 05 '20 at 10:55
  • For some reason, users created in such a way do not appear in the WooCommerce customers list in the admin. – Maštarija Jun 04 '21 at 19:22
  • Also, you can create an array with all these fields, then declare this: foreach ( $billing_address as $key => $value ) { update_user_meta( $user, $key, $value ); } foreach ( $shipping_address as $key => $value ) { update_user_meta( $user, $key, $value ); } – Jean Manzo Aug 06 '21 at 16:20
  • You can skip the username and password parameters, WC will create those automatically if it's empty. – passatgt Mar 03 '22 at 11:20
9

Before attempting to create a new user I see if that user already exists and update the existing user if found. the get_user_by(field,value) function returns the user object so if the user exists use their id to update meta fields or if not create a new one.

  $email = 'test@test.test';

        $address = array(
            'first_name' => 'Tester',
            'last_name'  => 'Test',
            'company'    => 'Testing, LLC',
            'email'      => $email,
            'phone'      => '777-777-777-777',
            'address_1'  => '310 S. Main Street',
            'address_2'  => '',
            'city'       => 'Las Vegas',
            'state'      => 'NV',
            'postcode'   => '89000',
            'country'    => 'US'
        );

        $default_password = wp_generate_password();
        $user = get_user_by('login', $email);

        if (!$user = $user->ID) $user = wp_create_user( $email, $default_password, $email );

        update_user_meta( $user, "billing_first_name", $address['first_name'] );
        update_user_meta( $user, "billing_last_name", $address['last_name']);
        update_user_meta( $user, "billing_company", $address['company'] );
        update_user_meta( $user, "billing_email", $address['email'] );
        update_user_meta( $user, "billing_address_1", $address['address_1']);
        update_user_meta( $user, "billing_address_2", $address['address_2'] );
        update_user_meta( $user, "billing_city", $address['city']);
        update_user_meta( $user, "billing_postcode", $address['postcode'] );
        update_user_meta( $user, "billing_country", 'US');
        update_user_meta( $user, "billing_state", $address['state'] );
        update_user_meta( $user, "billing_phone", $address['phone'] );
        update_user_meta( $user, "shipping_first_name", $address['first_name'] );
        update_user_meta( $user, "shipping_last_name", $address['last_name']);
        update_user_meta( $user, "shipping_company", $address['company'] );
        update_user_meta( $user, "shipping_address_1", $address['address_1']);
        update_user_meta( $user, "shipping_address_2", $address['address_2'] );
        update_user_meta( $user, "shipping_city", $address['city']);
        update_user_meta( $user, "shipping_postcode", $address['postcode'] );
        update_user_meta( $user, "shipping_country", 'US');
        update_user_meta( $user, "shipping_state", $address['state'] );
user1998497
  • 91
  • 2
  • 3
  • When you already have address field indexes partially, you could just run a `foreach` loop with just 3-4 line of codes instead of those 20+ lines of `update_user_meta` – Sohan Zaman Apr 20 '21 at 11:28
  • You should use: wc_create_new_customer( $email, $username, $password ); instead of: 'wp_create_user( $email, $default_password, $email );', right? – Tariq Apr 28 '21 at 21:06
  • Great, +1 for your validation. Also, you can create an array with all these fields, then declare this: foreach ( $billing_address as $key => $value ) { update_user_meta( $user, $key, $value ); } foreach ( $shipping_address as $key => $value ) { update_user_meta( $user, $key, $value ); } – Jean Manzo Aug 06 '21 at 16:23