php - 如何在邮件中显示 ACF 的字段?
问题描述
如果用户 Woocommerce 更改了他的数据,发送给管理员的邮件会收到有关这些更改的电子邮件。
我需要在这封信中显示我在 ACF 中创建的自定义字段。(在 ACF 中创建了一个问卷并将其放入 /edit-account 中)
这是完整的代码,基于 -在 woocommerce 自定义电子邮件通知中获取 ACF 自定义字段值。
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 = 'info@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_{$user_id}' ) . '</td></tr>';
$body .= '<tr><td>Age: </td><td>' . get_field( 'user_age', 'user_{$user_id}' ) . '</td></tr>';
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
//send email
if( wp_mail( $to, $subject, $body, $headers ) ){
//echo 'email sent';
}else{
//echo 'email NOT sent';
}
//exit();
}
}
new WooCommerceNotifyChanges();
}
不幸的是,未显示“user_phone”和“user_age”字段。这些自定义字段已创建并正确。它们显示在我的帐户中。在电子邮件中,没有。
即使这样我也写了,但该字段没有出现:
get_field( 'user_phone', $user_id )
解决方案
我已经研究过了,它wp_user_meta()
只是一个包装器get_metadata()
,并get_metadata()
缓存了它的结果。如果在执行之前对您的 WP 用户进行了编辑,它将被缓存。然后,稍后您要发送此电子邮件,您将已经在内存中保存了用户的“缓存”版本,这些是您返回的值。
你可以通过把它放在你的钩子的顶部来破坏缓存:
update_meta_cache( 'user', $user_id );
这应该会破坏缓存,所以当你这样做时:
get_user_meta($user_id, 'field_5b7e4f388fd11', $single=true);
您将获得更新后的值。
更新功能:
function woocommerce_send_notification( $user_id ){
$body = '';
$to = 'info@domain.com'; //address that will receive this email
$subject = 'One of your customers has updated their information.';
update_meta_cache( 'user', $user_id ); // Bust existing cached user data
$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_{$user_id}' ) . '</td></tr>';
$body .= '<tr><td>Age: </td><td>' . get_field( 'user_age', 'user_{$user_id}' ) . '</td></tr>';
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
//send email
if( wp_mail( $to, $subject, $body, $headers ) ){
//echo 'email sent';
}else{
//echo 'email NOT sent';
}
//exit();
}
推荐阅读
- iis - WebDav IIS - Access-Control-Allow-Origin:如何允许多个值
- java - UserDetailsService 是必需的
- node.js - 如何创建nodejs和mongo creat
- html - 如何在我的 wordpress 网站上为一行创建一个倾斜的分隔线?
- node.js - 在nodejs中查找2个坐标之间的速度
- ios - Xcode 10 如何使用带有 obfuscator-llvm 插件的新构建系统
- android - ChipGroup 2 路绑定适配器
- python - Pandas date_range 只有小时、分钟和秒
- css - 这个css中的两种字体大小是什么意思
- java - 在列表中创建具有不同大小的列表 - 主列表中包含子列表和子列表