WooCommerce Create account programmatically / through function
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
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'] );