This question related to user_register hook
I needed to add some extra fields on the user registration form on woocommerce i used Custom User Registration Fields - WooCommerce plugin
My question here is how can i access to the value of those added fields with add_action hooks?
This is what i have done so far,
add_action( 'user_register', 'new_contact', 10, 3 );
function new_contact( $user_id ) {
if ( isset( $_POST['first_name'] ) )
update_user_meta($user_id, 'first_name', $_POST['first_name']);
$customer = new WC_Customer( $user_id );
echo $customer;
wp_die();
}
the following is the echoed output:
{
"id":12,
"date_created":{"date":"2020-07-27 16:58:52.000000","timezone_type":1,"timezone":"+00:00"},
"date_modified":null,
"email":"[email protected]",
"first_name":""
,"last_name":"",
"display_name":"c",
"role":"customer",
"username":"c",
"billing":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","postcode":"","country":"","state":"","email":"","phone":""},
"shipping":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","postcode":"","country":"","state":""},
"is_paying_customer":false,
"meta_data":[
{"id":516,"key":"afreg_additional_1564","value":"c"},
{"id":517,"key":"afreg_additional_1565","value":"c"},
{"id":518,"key":"afreg_additional_1566","value":"c"},
{"id":519,"key":"afreg_additional_1555","value":"c"},
{"id":520,"key":"afreg_additional_1556","value":"c"},
{"id":521,"key":"afreg_additional_1557","value":"c"},
{"id":522,"key":"afreg_additional_1558","value":"California"},
{"id":523,"key":"afreg_additional_1559","value":"c"},
{"id":524,"key":"afreg_additional_1560","value":"6181001010"},
{"id":525,"key":"afreg_additional_1561","value":"c"},
{"id":526,"key":"_yoast_wpseo_profile_updated","value":"1595894332"}
]
}
What i can see is that the custom fields that i added are stored inside "meta_data"
How could i store each value in a variable?
You can access this
meta data
usingWC_Data
methods on theWC_Customer
Object like:get_meta_data()
to get an unprotected array ofWC_Meta_Data
objectsget_meta($meta_key)
To get the value from a specific meta keyTo access the data from
WC_Meta_Data
object, you will useget_data()
method.So here is working example of code:
So In your function:
Or you can use the Wordpress way using
get_user_meta()
function to get an array of all user meta data:To get the correct meta keys for your related user meta data, you can also look to
wp_usermeta
database table.