从Woocommerce中的woocommerce_created_customer挂钩获取用户元数据 [英] Get user meta data from woocommerce_created_customer hook in Woocommerce
问题描述
我正在使用woocommerce API创建客户API,并且可以正常工作,而新客户也可以在WP中正常创建.但是问题是当我使用钩子"woocommerce_created_customer"时,我无法获取用户元数据,例如名字",姓氏"等
I am using woocommerce API to create a customer API is working fine and the new customer is creating fine into WP. But the issue is when I am using a hook "woocommerce_created_customer" I am not able to get user meta data like "first name", "last name" etc
这是我的代码的一部分:
Here is a part of my code:
```
$this->loader->add_action( 'woocommerce_created_customer', $plugin_admin, 'add_user_to_zendesk', 50 );
public function add_user_to_zendesk( $user_id ) {
if ( isset( $user_id ) && ! empty( $user_id ) ) {
$user = get_user_by( 'id', $user_id )->data;
error_log('USER DATA'. print_r( $user, true ) );
error_log('USER METADATA'. print_r( get_user_meta($user_id), true ) );
}
}
```
这是日志文件中的响应
```
[01-Sep-2018 08:30:50 UTC] USER DATAstdClass Object
(
[ID] => 99
[user_login] => geek2
[user_pass] => XXXXXXXXX.
[user_nicename] => geek2
[user_email] => geek2@gmail.com
[user_url] =>
[user_registered] => 2018-09-01 08:30:49
[user_activation_key] =>
[user_status] => 0
[display_name] => geek2
)
[01-Sep-2018 08:30:50 UTC] USER METADATAArray
(
[nickname] => Array
(
[0] => geek2
)
[first_name] => Array
(
[0] =>
)
[last_name] => Array
(
[0] =>
)
[description] => Array
(
[0] =>
)
[rich_editing] => Array
(
[0] => true
)
[syntax_highlighting] => Array
(
[0] => true
)
[comment_shortcuts] => Array
(
[0] => false
)
[admin_color] => Array
(
[0] => fresh
)
[use_ssl] => Array
(
[0] => 0
)
[show_admin_bar_front] => Array
(
[0] => true
)
[locale] => Array
(
[0] =>
)
[wp_capabilities] => Array
(
[0] => a:1:{s:8:"customer";b:1;}
)
[wp_user_level] => Array
(
[0] => 0
)
[_yoast_wpseo_profile_updated] => Array
(
[0] => 1535790649
)
)
```
有人可以帮忙弄清楚这个问题吗...
Can someone please help to figure out the issue...
推荐答案
更新
首先,您可以尝试以下2种替代方法之一(在代码中添加):
First you could try one of this 2 alternatives (to add in your code):
1)使用WP_User
类的Wordpress方式:
1) The Wordpress way using WP_User
Class:
// Get an instance of the WP_User Object
$user = new WP_User( $user_id );
// Get the first name and the last name from WP_User Object
$first_name = $user->first_name;
$last_name = $user->last_name;
2)使用WC_Customer
类和方法的Woocommerce方式:
2) The Woocommerce way using WC_Customer
Class and methods:
// Get an instance of the WC_Customer Object
$customer = new WC_Customer( $customer_id );
// Get the first name and the last name from WC_Customer Object
$first_name = $customer->get_first_name();
$last_name = $customer->get_last_name();
然后查看 WC_Customer_Data_Store
源代码有一个有趣的动作挂钩可以使用: woocommerce_new_customer
具有唯一的参数$customer_id
.
Then looking at the WC_Customer_Data_Store
source code there is an interesting action hook that could be used: woocommerce_new_customer
that has a unique argument, the $customer_id
.
源代码中的
$customer->get_first_name()
和$customer->get_last_name()
出现在代码中,因此它们存在,您可以使用$customer_id
从WC_Customer
实例对象中获取它们,如下所述……
In the source code
$customer->get_first_name()
and$customer->get_last_name()
appear in the code, so they exist and you can get them from theWC_Customer
instance Object using the$customer_id
, as described here below…
最后可能性
由于用户元数据似乎被延迟并且Woocommerce似乎正在使用WordPress函数,因此您可以尝试使用WordPress钩子
As the user meta data seems to be delayed and Woocommerce seems to be using WordPress functions, you could try to use the WordPress hook add_user_meta
which has 3 arguments:
-
$user_id
-
$meta_key
-
$meta_value
$user_id
$meta_key
$meta_value
但是我不知道如何将其包含在您的代码中,希望您可以从WP_User
对象或WC_Customer
对象中获取数据.
But I don't know how to include it in your code, hoping that you could get the data from WP_User
Object or WC_Customer
Object.
这篇关于从Woocommerce中的woocommerce_created_customer挂钩获取用户元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!