gpt4 book ai didi

php - 如何在邮件中显示ACF的字段或字段键?

转载 作者:行者123 更新时间:2023-11-29 16:58:57 25 4
gpt4 key购买 nike

我使用 ACF 插件和 ACF for WooCommerce。如果用户 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' ), 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”和其他字段不会显示。

例如,“ACF for WooCommerce”插件将数据库中的“user_phone”字段保存为meta_key:“field_5b7e4f388fd11”和meta_value:“+79998006655”。

enter image description here

在我的代码中,该字段显示如下:

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

当我输入字段键“field_5b7e4f388fd11”而不是字段名称时...

get_field( 'field_5b7e4f388fd11', "user_{$user_id}" )

我收到一封包含空白字段的电子邮件。

如何解决这个问题?

<小时/>

更新:我可以直接使用 get_user_meta:

get_user_meta($user_id, 'field_5b7e4f388fd11', $single=true);

此选项有效,但如果我用新的“+78009995566”替换现有的字段值,例如“+79998006655”,那么所有内容都会正确存储在数据库中,并且旧版本的“+79998006655”会出现发送至管理员的电子邮件。

如果自定义字段是复选框,则它会正确存储在数据库中,但管理员电子邮件中仅显示“Array”。

是的,发给管理员的电子邮件已经很久了。

如何解决这个问题?

最佳答案

不需要加大括号

试试这个代码:

get_field( 'user_age', "user_$user_id" ) 

更新1:

更多信息请访问 ACF Official docs .

ACF 文档中的示例:

// This example will retrieve a field value from a user with an ID of 1.
$variable = get_field('field_name', 'user_1');

更新2:

试试这个:

get_field( 'user_phone', 'user_' . $user_id )

关于php - 如何在邮件中显示ACF的字段或字段键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52402676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com