Get user meta data from woocommerce_created_customer hook in Woocommerce

*爱你&永不变心* 提交于 2020-04-13 07:12:27

问题


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 ) );
         }
}
```

Here is a response in log file

```

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


回答1:


Update

First you could try one of this 2 alternatives (to add in your code):

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

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.

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…


Last possibility

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

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.



来源:https://stackoverflow.com/questions/52126496/get-user-meta-data-from-woocommerce-created-customer-hook-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!