WooCommerce Create account programmatically / through function

Stefan Dunn picture Stefan Dunn · Sep 24, 2015 · Viewed 10.1k times · Source

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

Answer

Anand Shah picture Anand Shah · Sep 24, 2015

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