Reputation: 5513
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).
Upvotes: 16
Views: 19046
Reputation: 101
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 = '[email protected]';
$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'] );
Upvotes: 10
Reputation: 14913
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
Shipping
Upvotes: 39