在woocommerce自定义电子邮件通知中获取ACF自定义字段值 [英] Get ACF custom fields values in woocommerce custom email notification
问题描述
如果用户Woocommerce更改了其数据,则发给管理员的电子邮件将收到有关这些更改的电子邮件。
If the user Woocommerce changes his data, a mail to the administrator receives a email about these changes.
我需要在这封信中显示我自定义的字段在ACF中创建。 (在ACF中创建了一个调查表并将其放入/ edit-account)
I need to show in this letter the custom fields that I created in the ACF. (In ACF created a questionnaire and put it into a /edit-account)
以下是完整的代码,基于-客户更改地址为WooCommerce时发送通知电子邮件
Here is the complete code, based on - Send notification email when customer changes address WooCommerce
if( !class_exists('WooCommerceNotifyChanges') ){
class WooCommerceNotifyChanges{
function __construct(){
// customer saves main account data
add_action('woocommerce_save_account_details', array( $this, 'woocommerce_send_notification' ));
}
function woocommerce_send_notification(){
$body = '';
$to = 'email@domain.com'; //address that will receive this email
$subject = 'One of your customers has updated their information.';
$curr_user = wp_get_current_user();
$user_id = $curr_user->ID;
$curr_username = $curr_user->data->user_login;
$body .= '<table>';
$body .= '<tr><td><strong>Account</strong></td></tr>';
$body .= '<tr><td>Username: </td><td>' . $curr_username . '</td></tr>';
$body .= '<tr><td>First name: </td><td>' . get_user_meta( $user_id, 'billing_first_name', true ). '</td></tr>';
$body .= '<tr><td>Last name: </td><td>' . get_user_meta( $user_id, 'billing_last_name', true ) . '</td></tr>';
$body .= '<tr><td>Phone: </td><td>' . get_field('user_phone') . '</td></tr>';
$body .= '<tr><td>Age: </td><td>' . get_field('user_age') . '</td></tr>';
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
}
} new WooCommerceNotifyChanges();
}
不幸的是,未显示自定义字段。帐户中的所有字段均已填写,我收到了一封有关更改用户数据的电子邮件。但是自定义字段不会显示在电子邮件中。
Unfortunately, the custom field is not displayed. All fields in my account have been filled in, I received a email about changing the user's data. But custom fields are not shown in the email.
我尝试了以下选项:
$body .= '<tr><td>Phone: </td><td>' . get_field('user_phone', 'user_1') . '</td></tr>';
$body .= '<tr><td>Phone: </td><td>' . get_field('user_phone', $user_id) . '</td></tr>';
我会很高兴您的帮助。
推荐答案
首先,您不能在逻辑上将 wp_get_current_user()
用于电子邮件通知或后端订单,因为没有当前用户
First, you can't use logically wp_get_current_user()
for email notifications or backend orders, as there is no a current user anymore.
但是如果您看看 woocommerce_save_account_details
挂钩源代码,您会注意到 $ user_id
作为挂钩参数传递,您可以在相关函数中使用它,如下所示:
But if you have a look to woocommerce_save_account_details
hook source code you will notice that $user_id
is passed as a hook argument and you can use it in the related function, as following:
if( !class_exists('WooCommerceNotifyChanges') ){
class WooCommerceNotifyChanges{
function __construct(){
// customer saves main account data
add_action('woocommerce_save_account_details', array( $this, 'woocommerce_send_notification' ), 15, 1 );
}
function woocommerce_send_notification( $user_id ){
$body = '';
$to = 'email@domain.com'; //address that will receive this email
$subject = 'One of your customers has updated their information.';
$user = new WP_User( $user_id );
$user_name = $user->user_login;
$body .= '<table>';
$body .= '<tr><td><strong>' . __("Account") . '</strong></td></tr>';
$body .= '<tr><td>Username: </td><td>' . $user_name . '</td></tr>';
$body .= '<tr><td>First name: </td><td>' . $user->billing_first_name . '</td></tr>';
$body .= '<tr><td>Last name: </td><td>' . $user->billing_last_name . '</td></tr>';
$body .= '<tr><td>Phone: </td><td>' . get_field( 'user_phone', $user_id ) . '</td></tr>';
$body .= '<tr><td>Age: </td><td>' . get_field( 'user_age', $user_id ) . '</td></tr>';
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
}
}
new WooCommerceNotifyChanges();
}
现在应该可以使用了。
这篇关于在woocommerce自定义电子邮件通知中获取ACF自定义字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!