Woocommerce_Created_Customer挂钩发送带有其他字段无效的电子邮件

时间:2018-12-30 19:34:30

标签: php wordpress woocommerce hook-woocommerce

使用 woocommerce_created_customer 钩子发送包含用户名,密码和用户自定义电子邮件的自定义电子邮件时,一切正常。

但是,如果我尝试添加以下字段:名字,姓氏和公司名称。它不会显示接收电子邮件中的字段。而且我找不到问题所在!

所以基本上我需要的是:用户名,密码,电子邮件,名字,姓氏,电话号码和公司名称。

这是我目前所得到的,但是仅显示用户名,密码,电子邮件和客户/用户ID。

    // send Customer details
add_action( 'woocommerce_created_customer', 'create_cd_account', 10, 3 );

function create_cd_account( $customer_id, $new_customer_data, $password_generated ) {

// Get the New Customer's details
$username   = $new_customer_data['user_login'];
$password   = $new_customer_data['user_pass'];
$email      = $new_customer_data['user_email'];
$role       = $new_customer_data['role'];


// Get an instance of the WC_Customer Object
$user = new WC_Customer( $customer_id );

// Get the first name and the last name from WP_User Object 
$first_name = $user->first_name;
$last_name  = $user->last_name;
$phone      = $user->billing_phone;
$company    = $user->billing_company;


// Continue and send the information
// Testing sending email
$to = 'myaddress@mydomain.com';
$subject = 'Test email - does it work?';

$message = "Hello, {$first_name}\n\n";
$message .= "Here are your login details:\n\n";
$message .= "Your company is: {$company}\n\n";
$message .= "Your name is: {$first_name . ' ' . $last_name}\n\n";
$message .= "Your customer/user ID is: {$user->ID}\n\n";
$message .= "test extra user ID: {$customer_id}\n\n";
$message .= "Your username is: {$username}\n\n";    
$message .= "Your password is: {$password}\n\n";
$message .= "Your email is: {$email}\n\n";
$message .= "Your role is: {$role}\n\n";
$message .= "Your phone number is: {$phone}\n\n";

add_filter( 'wp_mail_from', $from_func = function ( $from_email ) { return 'info@mydomain.com'; } );
add_filter( 'wp_mail_from_name', $from_name_func = function ( $from_name ) { return 'My Wordpress Website'; } );

// send email
wp_mail( $to, $subject, $message );

remove_filter( 'wp_mail_from', $from_func );
remove_filter( 'wp_mail_from_name', $from_name_func );
}

我还尝试了以下操作(没有运气):

$user = get_user_by( 'id', $customer_id );

$customer = get_userdata($customer_id);

我希望有人可以告诉我哪里出了问题?以及如何使其工作。

1 个答案:

答案 0 :(得分:0)

您的代码帮助了我,但我认为我的代码可以帮助您。我只需要名字和姓氏,所以我这样使用$_POST['first_name']。我认为这也对您有效。只需将名字替换为输入的名称