从 Woocommerce 中的 woocommerce_created_customer 挂钩获取用户元数据

Get user meta data from woocommerce_created_customer hook in Woocommerce

我正在使用 woocommerce API 创建客户 API 工作正常,新客户正在创建 WP。但问题是当我使用钩子 "woocommerce_created_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 ) );
         }
}
```

这是日志文件中的响应

```

[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
        )

)
```

有人可以帮忙解决问题吗...

更新

首先,您可以尝试这 2 个备选方案之一 (添加到您的代码中)

1) 使用 WP_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_Customer Class 和方法的 Woocommerce 方式:

// 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 source code there is an interesting action hook that could be used: woocommerce_new_customer,即 $customer_id

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 the WC_Customer instance Object using the $customer_id, as described here below…


最后的可能

由于用户元数据似乎被延迟并且 Woocommerce 似乎正在使用 WordPress 功能,您可以尝试使用具有 3 个参数的 WordPress 挂钩 add_user_meta

  • $user_id
  • $meta_key
  • $meta_value

但我不知道如何将它包含在您的代码中,希望您可以从 WP_User 对象或 WC_Customer 对象中获取数据。