vico vico - 10 days ago 6
PHP Question

Wordpress Woocommerce - User Billing Meta Update

I currently have this working example.
Everything but the woocommerce meta does not get updated.

Is my syntax incorrect? ( rgar is a gravity form function that returns escaped form value )

$user_id = wp_create_user( $username, $password, rgar( $entry, '4' ) );
wp_update_user( array(
'ID' => $user_id,
'role' => 'customer', //works
'first_name' => rgar( $entry, '1.3' ), //works
'last_name' => rgar( $entry, '1.6' ), //works
'billing_first_name' => rgar( $entry, '1.3' ),
'billing_last_name' => rgar( $entry, '1.6' ),
'show_admin_bar_front' => 'false', //works
'billing_email' => rgar( $entry, '4' ),
'billing_address_1' => rgar( $entry, '3.1' ),
'billing_city' => rgar( $entry, '3.3' ),
'billing_state' => rgar( $entry, '3.4' ),
'billing_postcode' => rgar( $entry, '3.5' ),
'billing_phone' => rgar( $entry, '6' )
));
update_field('field_5629452b8c7de', rgar( $entry, '1.3' ) . ' ' . rgar( $entry, '1.6' ), 'user_' . $user_id); //works
update_field('field_5629455f8c7df', rgar( $entry, '9' ), 'user_' . $user_id); //works
update_field('field_569e4be42ab47', rgar( $entry, '8' ), 'user_' . $user_id); //works
update_field('field_569e4c192ab48', str_replace('-', '', rgar( $entry, '7' )), 'user_' . $user_id); //works

Answer

Most of the things you are trying to update in wp_update_user() are user_meta fields, rather than user fields.

... So, you need to use:

update_user_meta( $user_id, $meta_key, $meta_value, $prev_value );

There are a few user_meta fields that wp_update_user() will automatically recognise and save in the right way, but Woocommerce's custom ones are not included there.

So you need to update your code accordingly, to use update_user_meta(). E.g.:

update_user_meta( $user_id, 'billing_postcode', rgar( $entry, '3.5' ) );

Check here to see all user_meta fields that you can pass to the wp_update_user() function (same as those that you can pass to the wp_insert_user() function -https://codex.wordpress.org/Function_Reference/wp_insert_user#Notes