在woocommerce自定义电子邮件通知中获取ACF自定义字段值

时间:2018-08-06 20:57:52

标签: php wordpress woocommerce advanced-custom-fields hook-woocommerce

如果用户Woocommerce更改了其数据,则发给管理员的电子邮件将收到有关这些更改的电子邮件。

我需要在这封信中显示在ACF中创建的自定义字段。 (在ACF中创建了一个调查表并将其放入/ edit-account)

这是完整的代码,基于-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(); 
} 

不幸的是,未显示自定义字段。帐户中的所有字段均已填写,我收到了一封有关更改用户数据的电子邮件。但是自定义字段不会显示在电子邮件中。

我尝试了该选项:

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

我会很高兴您的帮助。

2 个答案:

答案 0 :(得分:1)

首先,您不能在逻辑上$user_id用于电子邮件通知或后端订单,因为已经没有当前用户了。

但是,如果您看一下woocommerce_save_account_details hook source code,将会发现 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(); } 是作为钩子参数传递的,您可以在相关函数中使用它,如下所示:

jni/matt_extend_NativeTest.h

现在应该可以了。

答案 1 :(得分:0)

要从ACF中的用户记录中获取字段,您不仅要传递用户ID,还必须在以“ user_”开头的字符串中传递它,因此该字符串应为:"user_{$user_id}"和整个字符串因此,事情将是get_field( 'user_age', "user_{$user_id}" )

查看文档:{​​{3}}