Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从woocommerce获取用户元数据\u在woocommerce中创建的\u客户挂钩_Php_Wordpress_Woocommerce_Hook Woocommerce_Woocommerce Rest Api - Fatal编程技术网

Php 从woocommerce获取用户元数据\u在woocommerce中创建的\u客户挂钩

Php 从woocommerce获取用户元数据\u在woocommerce中创建的\u客户挂钩,php,wordpress,woocommerce,hook-woocommerce,woocommerce-rest-api,Php,Wordpress,Woocommerce,Hook Woocommerce,Woocommerce Rest Api,我正在使用WooCommerceAPI创建一个客户API,该API运行良好,新客户正在创建一个可湿性粉剂。但问题是,当我使用钩子“woocommerce\u created\u customer”时,我无法获取诸如“first name”、“last name”等用户元数据 以下是我代码的一部分: ``` $this->loader->add_action( 'woocommerce_created_customer', $plugin_admin, 'add_user_to_zen

我正在使用WooCommerceAPI创建一个客户API,该API运行良好,新客户正在创建一个可湿性粉剂。但问题是,当我使用钩子“woocommerce\u created\u customer”时,我无法获取诸如“first name”、“last name”等用户元数据

以下是我代码的一部分:

```
$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 ) );
         }
}
```
下面是日志文件中的响应

```


有人能帮忙解决这个问题吗…

更新

首先,您可以尝试以下两种选择之一(添加到代码中):

1) 使用
WP\u User
Class的Wordpress方式:

// 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\u Customer
类和方法的方式:

// 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();

然后看看源代码,可以使用一个有趣的操作挂钩:它有一个唯一的参数,
$customer\u id

在源代码中,
$customer->get_first_name()
$customer->get_last_name()
出现在代码中,因此它们存在,您可以使用
$customer\u id
WC_customer
实例对象获取它们,如下所述


最后一种可能性

由于用户元数据似乎被延迟,Woocommerce似乎正在使用WordPress函数,您可以尝试使用WordPress挂钩,它有3个参数:

  • $user\u id
  • $meta_键
  • $meta\u值

但我不知道如何在你的代码中包含它,希望您可以从
WP\u User
Object或
WC\u Customer
Object获取数据。

您是否已为创建的用户ID签入
WP\u usermeta
数据库表……可能没有创建用户元数据或没有为元键
first\u name
last\u name
创建元数据。你用什么来创建用户?是的,过了一段时间我通过下面的代码get_user_meta检查了usermeta(96,“first_name”,true);它返回了一个名字。我认为在元插入中有一些延迟,钩子在此之前正在调用。有什么解决方案可以克服这个问题吗?你可以尝试使用
woocommerce\u new\u customer
动作挂钩,它有$customer\u id作为唯一的函数参数…谢谢你,第二个解决方案对我来说很好。
// 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();