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

问题描述 投票:3回答:1

我正在使用woocommerce API来创建客户API工作正常,新客户正在创建WP。但问题是当我使用钩子“woocommerce_created_customer”时,我无法获得用户元数据,如“名字”,“姓氏”等

这是我的代码的一部分:

```
$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] => [email protected]
    [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
        )

)
```

有人可以帮助找出问题...

php wordpress woocommerce hook-woocommerce woocommerce-rest-api
1个回答
2
投票

更新

首先,您可以尝试以下两种方法之一(添加代码):

1)使用WP_User类的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类和方法的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源代码,有一个有趣的动作钩子可以使用:woocommerce_new_customer有一个独特的参数,$customer_id

在源代码中,$customer->get_first_name()$customer->get_last_name()出现在代码中,因此它们存在,您可以使用WC_Customer$customer_id实例Object获取它们,如下所述...


最后的可能性

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

  • $user_id
  • $meta_key
  • $meta_value

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

© www.soinside.com 2019 - 2024. All rights reserved.