Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Create account programmatically / through function

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

like image 450
Stefan Dunn Avatar asked Sep 24 '15 09:09

Stefan Dunn


2 Answers

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
like image 95
Anand Shah Avatar answered Nov 10 '22 11:11

Anand Shah


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'] );
like image 23
user1998497 Avatar answered Nov 10 '22 12:11

user1998497