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

Display Customer Firstname and User Meta to New Woocomerce account email

我想获取新注册人的名字并将其显示在我的电子邮件模板中:

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

但它没有返回值。有什么想法吗?

谢谢

您可以使用用户 ID 尝试 get_userdata() wordpress 函数,以获取用户对象。使用此用户对象,您可以通过以下方式获取所有其他数据:

$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


对于其他电子邮件通知,一旦创建了一次订单,您可以从订单 ID 轻松获取以下数据:

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

问题是用户是如何创建的,这是我创建用户的代码:

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

为了解决这个问题,我将代码更改为:

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