【问题标题】:How to show the fields of ACF in email?如何在邮件中显示 ACF 的字段?
【发布时间】:2019-01-28 09:45:11
【问题描述】:

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

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

这是完整的代码,基于 - Get ACF custom fields values in woocommerce custom email notification.

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 )

【问题讨论】:

  • $user_id 真的包含 ID 吗?
  • 我怎么知道?
  • 尝试回显 $user_id - 它返回什么
  • $user_id 有效,因为它是一个钩子参数,您应该尝试使用真实的用户 ID(a数值而不是动态变量),看看你是否得到了一些输出。如果不是,则表示 ACF 没有保存任何价值。
  • get_field( 'user_phone', 13) user_id13 产生一个空结果((

标签: php wordpress woocommerce advanced-custom-fields email-notifications


【解决方案1】:

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

【讨论】:

  • 感谢您的回答。告诉我如何输入update_meta_cache('user', $user_id);?你能在我的代码示例中展示这个吗?
  • @Dmitry 把它放在$user = new WP_User( $user_id );的上方
  • 我把代码放在你说的地方。都没有变化。显然仍在缓存中。
  • 那么复选框呢?数据库中的一切都是正确的,并且在给管理员的电子邮件中显示“数组”(
  • @Dmitry 哪个字段是复选框?检查返回的数组的值。如果是 Age,请执行 $age = get_user_meta($user_id, 'user_age', $single=true); var_dump($age); 之类的操作来找出 $age 数组中的内容。您可能只需要输出 $age['value']get_user_meta($user_id, 'user_age', true)['value'] 或类似的东西。
【解决方案2】:

我认为问题在于,每当您传递get_field() 的第二个参数时,如何解析字符串。通过使用单引号,PHP 将其解释为字符串文字,因此您将无法将任何 PHP 变量传递给字符串。例如:

echo 'user_{$user_id}'; // Outputs: user_{$user_id}

但是如果用户使用双引号,PHP 会自动转义并处理:

echo "user_{$user_id"; // Outputs: user_123

所以每当你使用get_field() 时,它应该是这样的:

get_field( 'user_phone', "user_{$user_id}" );

【讨论】:

  • 事实证明,这个插件“ACF for Woocommerce”将字段数据保存为元键:“field_5b7e4f388fd11”和元值:“+79998006655”。告诉我如何正确显示代码中的字段? get_field( 'field_5b7e4f388fd11', "user_{$user_id}" ) 这由于某种原因不起作用。电子邮件为空字段((
猜你喜欢
  • 2019-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-09-01
  • 2021-11-16
  • 2017-01-01
  • 2016-02-04
  • 2018-04-11
相关资源
最近更新 更多