在 WooCommerce 挂钩中访问客户自定义用户元数据
Access customer custom user meta data in WooCommerce hooks
这个问题与user_register hook
相关
我需要在 woocommerce 的用户注册表中添加一些额外的字段
我用了 Custom User Registration Fields - WooCommerce plugin
我的问题是如何使用 add_action 挂钩访问那些添加的字段的值?
这是我到目前为止所做的,
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();
}
回显输出如下:
{
"id":12,
"date_created":{"date":"2020-07-27 16:58:52.000000","timezone_type":1,"timezone":"+00:00"},
"date_modified":null,
"email":"c@hotmail.com",
"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"}
]
}
我可以看到我添加的自定义字段存储在“meta_data”
中
如何将每个值存储在一个变量中?
您可以在 WC_Customer
对象上使用 WC_Data
methods 访问此 meta data
,例如:
get_meta_data()
获取 WC_Meta_Data
个对象的未保护数组
get_meta($meta_key)
从特定元键获取值
要从 WC_Meta_Data
对象访问数据,您将使用 get_data()
method。
所以这里是代码的工作示例:
$user_id = 12; // For user ID 12
// Get an instance of the WC_Customer Object from user Id
$customer = new WC_Customer( $user_id );
// Get the meta value from a specific meta key
$meta_value = $customer->get_meta('afreg_additional_1558');
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an unprotected array of `WC_Meta_Data` objects
$user_meta_data = $customer->get_meta_data();
// Loop through `WC_Meta_Data` objects
foreach( $customer->get_meta_data() as $meta_data ){
// get an unprotected array of the current WC_Meta_Data object data
$meta_data_array = $meta_data->get_data();
$meta_key = $meta_data_array['key']; // The meta key
$meta_value = $meta_data_array['value']; // The meta value
$meta_value = is_array($meta_value) ? implode(' | ', $meta_value) : $meta_value;
// Testing output
echo $meta_key . ': ' . $meta_value . '<br>';
}
所以在你的函数中:
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']);
// Get an instance of the WC_Customer Object from user Id
$customer = new WC_Customer( $user_id );
// Get the meta value from a specific meta key
$meta_value = $customer->get_meta('afreg_additional_1558');
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an unprotected array of `WC_Meta_Data` objects
$user_meta_data = $customer->get_meta_data();
// Loop through `WC_Meta_Data` objects
foreach( $customer->get_meta_data() as $meta_data ){
// get an unprotected array of the current WC_Meta_Data object data
$meta_data_array = $meta_data->get_data();
$meta_key = $meta_data_array['key']; // The meta key
$meta_value = $meta_data_array['value']; // The meta value
$meta_value = is_array($meta_value) ? implode(' | ', $meta_value) : $meta_value;
// Testing output
echo $meta_key . ': ' . $meta_value . '<br>';
}
// echo $customer;
wp_die();
}
或者您可以使用 get_user_meta()
function 的 Wordpress 方式来获取所有用户元数据的数组:
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']);
// Get an instance of the WP_User Object from user Id
$user = new WP_User( $user_id );
// Get the meta value from a specific meta key
$meta_value = $user->afreg_additional_1558;
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// OR using
$meta_value = get_user_meta( $user_id, 'afreg_additional_1560', true );
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an array of all user meta data
$user_meta_data = get_user_meta( $user_id );
// Testing output meta data
print_r( $user_meta_data );
wp_die();
}
要为您的相关用户元数据获取正确的元键,您还可以查看 wp_usermeta
数据库 table。
To finish, as you can see now, it's easy to store each value in a variable, But your third party plugin, doesn't make things easy and explicit as each meta_key is a kind of internal id.
这个问题与user_register hook
相关我需要在 woocommerce 的用户注册表中添加一些额外的字段 我用了 Custom User Registration Fields - WooCommerce plugin
我的问题是如何使用 add_action 挂钩访问那些添加的字段的值?
这是我到目前为止所做的,
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();
}
回显输出如下:
{
"id":12,
"date_created":{"date":"2020-07-27 16:58:52.000000","timezone_type":1,"timezone":"+00:00"},
"date_modified":null,
"email":"c@hotmail.com",
"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"}
]
}
我可以看到我添加的自定义字段存储在“meta_data”
中如何将每个值存储在一个变量中?
您可以在 WC_Customer
对象上使用 WC_Data
methods 访问此 meta data
,例如:
get_meta_data()
获取WC_Meta_Data
个对象的未保护数组get_meta($meta_key)
从特定元键获取值
要从 WC_Meta_Data
对象访问数据,您将使用 get_data()
method。
所以这里是代码的工作示例:
$user_id = 12; // For user ID 12
// Get an instance of the WC_Customer Object from user Id
$customer = new WC_Customer( $user_id );
// Get the meta value from a specific meta key
$meta_value = $customer->get_meta('afreg_additional_1558');
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an unprotected array of `WC_Meta_Data` objects
$user_meta_data = $customer->get_meta_data();
// Loop through `WC_Meta_Data` objects
foreach( $customer->get_meta_data() as $meta_data ){
// get an unprotected array of the current WC_Meta_Data object data
$meta_data_array = $meta_data->get_data();
$meta_key = $meta_data_array['key']; // The meta key
$meta_value = $meta_data_array['value']; // The meta value
$meta_value = is_array($meta_value) ? implode(' | ', $meta_value) : $meta_value;
// Testing output
echo $meta_key . ': ' . $meta_value . '<br>';
}
所以在你的函数中:
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']);
// Get an instance of the WC_Customer Object from user Id
$customer = new WC_Customer( $user_id );
// Get the meta value from a specific meta key
$meta_value = $customer->get_meta('afreg_additional_1558');
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an unprotected array of `WC_Meta_Data` objects
$user_meta_data = $customer->get_meta_data();
// Loop through `WC_Meta_Data` objects
foreach( $customer->get_meta_data() as $meta_data ){
// get an unprotected array of the current WC_Meta_Data object data
$meta_data_array = $meta_data->get_data();
$meta_key = $meta_data_array['key']; // The meta key
$meta_value = $meta_data_array['value']; // The meta value
$meta_value = is_array($meta_value) ? implode(' | ', $meta_value) : $meta_value;
// Testing output
echo $meta_key . ': ' . $meta_value . '<br>';
}
// echo $customer;
wp_die();
}
或者您可以使用 get_user_meta()
function 的 Wordpress 方式来获取所有用户元数据的数组:
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']);
// Get an instance of the WP_User Object from user Id
$user = new WP_User( $user_id );
// Get the meta value from a specific meta key
$meta_value = $user->afreg_additional_1558;
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// OR using
$meta_value = get_user_meta( $user_id, 'afreg_additional_1560', true );
echo 'Value: ' . $meta_value . '<br>'; //Display the value
// Get an array of all user meta data
$user_meta_data = get_user_meta( $user_id );
// Testing output meta data
print_r( $user_meta_data );
wp_die();
}
要为您的相关用户元数据获取正确的元键,您还可以查看 wp_usermeta
数据库 table。
To finish, as you can see now, it's easy to store each value in a variable, But your third party plugin, doesn't make things easy and explicit as each meta_key is a kind of internal id.