首页 > 解决方案 > 如何在邮件中显示 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”字段。这些自定义字段已创建并正确。它们显示在我的帐户中。在电子邮件中,没有。

这是设置 ACF 的屏幕截图图片在这里

我的账户(截图)

即使这样我也写了,但该字段没有出现:

get_field( 'user_phone', $user_id )

标签: phpwordpresswoocommerceadvanced-custom-fieldsemail-notifications

解决方案


我已经研究过了,它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();
}

推荐阅读