2

Im trying to create a new user programmatically trough a form but Im having problem with getting the phone numer and country set trough wp_create_user - why wont it take the values? First and last name works as expected.

Relevant code:

$user_id = wp_create_user( $username, $random_password, $user_email ); 
    wp_update_user([
    'ID' => $user_id,
     'first_name' => rgar( $entry, '20.3' ),
     'last_name'  => rgar( $entry, '20.6' ),
     'phone'      => rgar( $entry, '16' ),
     'country'  => rgar( $entry, '24.6' )
    ]);
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Jerry
  • 1,069
  • 2
  • 13
  • 31

2 Answers2

2

In with WooCommerce the phone and the country are billing fields so the right user meta keys are:

  • billing_country (Remember that you need to set a valid country code)
  • billing_phone

You will also need to set the billing_email, billing_first_name and billing_last_name

So your code is going to be instead, replacing also your wp_create_user() function by:

    $username = rgar( $entry, '20.3' );
    $email    = rgar( $entry, '10' );
    $password = wp_generate_password( 12, false );

    $user_data = array(
        'user_login' => $username,
        'user_pass'  => $password,
        'user_email' => $email,
        'role'       => 'customer',
        'first_name' => rgar( $entry, '20.3' ),
        'last_name'  => rgar( $entry, '20.6' ),
    );

    $user_id  = wp_insert_user( $user_data ); // Create user with specific user data

Then to add the WooCommerce user data there is 2 ways:

1). Using WC_Customer Object and methods:

    $customer = new WC_Customer( $user_id ); // Get an instance of the WC_Customer Object from user Id

    $customer->set_billing_first_name( rgar( $entry, '20.3' ) );
    $customer->set_billing_last_name( rgar( $entry, '20.6' ) );
    $customer->set_billing_country( rgar( $entry, '24.6') );
    $customer->set_billing_phone( rgar( $entry, '16' ) );
    $customer->set_billing_email( $email );

    $customer->save(); // Save data to database (add the user meta data)

2) Or using WordPress update_user_meta() function (the old way):

update_user_meta( $user_id, 'billing_first_name', rgar( $entry, '20.3') );
update_user_meta( $user_id, 'billing_last_name', rgar( $entry, '20.6') );
update_user_meta( $user_id, 'billing_country', rgar( $entry, '24.6') );
update_user_meta( $user_id, 'billing_phone', rgar( $entry, '16') );
update_user_meta( $user_id, 'billing_email', $email );
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you! Getting closer. With this I get the number set correctly, but the country doesn't update for some reason – Jerry Nov 03 '20 at 21:09
  • Hmm how do I get the country code set for the selected country (the country gets selected from a dropdown county field) – Jerry Nov 03 '20 at 21:14
  • 1
    Thank you - I read up a bit on it and I see the problem - seems Gravity forms has a filter hook to convert the country names at least. Ill give it a shot first before I create a new question. Thanks again! – Jerry Nov 03 '20 at 21:22
0

You can add country and phone as a User meta and save it in the User Meta table:

add_user_meta( $user_id, 'country', rgar( $entry, '24.6'));
add_user_meta( $user_id, 'phone', rgar( $entry, '16'));

WordPress Codex : Click Here

Harshana
  • 5,151
  • 1
  • 17
  • 27
  • Thanks! Dont get it to work though, should I call it in the array somehow? My php is not that strong.. – Jerry Nov 03 '20 at 19:25