向新的Woocommerce帐户电子邮件显示客户名和用户元

I want to get the the new registrant first name and display it on my email template:

$firstname = get_user_meta($userid,'first_name',true);
echo '<pre>';
print_r($firstname);
echo '</pre>'

But it is not returning the value. Any ideas?

Thanks

The issue was how the user is created and this is my code for creating the user:

$user = wc_create_new_customer($email,$email,$password);

In order to fix that I change the code to:

$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
    'user_login'  => $email,
    'user_pass' => $password  ,
    'first_name'  =>  $firstname ,
    'last_name' =>  $lastname ,
    'user_email'  =>$email ,
    'display_name'  =>  $firstname . ' ' . $lastname  ,
    'nickname'  => $firstname . ' ' . $lastname ,
    'role'    => 'customer'
) );
$user_id = wp_insert_user( $new_customer_data );

You could try get_userdata() wordpress function with the User ID, to get the user object. With this user object you can get all other data this way:

$user_data = get_userdata( $userid );
echo 'Username: ' . $user_data->user_login . '<br>';
echo 'First name: ' . $user_data->first_name . '<br>';
echo 'Last name: ' . $user_data->last_name . '<br>';
echo 'User roles: ' . implode(', ', $user_data->roles) . '<br>';
echo 'User ID: ' . $user_data->ID . '<br>';

// To look at the available User raw data:
echo '<pre>'; print_r($user_data); echo '</pre>';

// To look at the available User raw Meta data
echo '<pre>'; print_r(get_post_meta( $userid )); echo '</pre>';

To get the First name and last name, your registering form need to have additionally that 2 fields


For other emails notification, once an order has been created one time, you can get easily from the Order ID this data:

// In case that you have only the $order object
if(empty($order_id)) 
    $order_id = $order->id;

$customer_id = get_post_meta( $order_id, '_customer_user', true);
$billing_first_name = get_post_meta( $order_id, '_billing_first_name', true);
$billing_last_name = get_post_meta( $order_id, '_billing_last_name', true);

// Displaying "First name"
echo 'First name: '. $billing_first_name;