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

Php 在自定义电子邮件通知中获取ACF自定义字段值,php,wordpress,woocommerce,advanced-custom-fields,hook-woocommerce,Php,Wordpress,Woocommerce,Advanced Custom Fields,Hook Woocommerce,如果用户更改了数据,发送给管理员的邮件将收到一封关于这些更改的电子邮件 我需要在这封信中显示我在ACF中创建的自定义字段。(在ACF中创建一份调查问卷并将其放入一个/编辑帐户) 以下是完整的代码,基于- 如果(!class_存在('wooCommerconTifyChanges')){ 类别转换{ 函数_u构造(){ //客户保存主帐户数据 添加_操作('woocommerce_save_account_details',数组('this,'woocommerce_send_notificati

如果用户更改了数据,发送给管理员的邮件将收到一封关于这些更改的电子邮件

我需要在这封信中显示我在ACF中创建的自定义字段。(在ACF中创建一份调查问卷并将其放入一个/编辑帐户)

以下是完整的代码,基于-

如果(!class_存在('wooCommerconTifyChanges')){
类别转换{
函数_u构造(){
//客户保存主帐户数据
添加_操作('woocommerce_save_account_details',数组('this,'woocommerce_send_notification');
}
功能:发送通知(){
$body='';
$to$email@domain.com“;//将接收此电子邮件的地址
$subject='您的一位客户已更新了他们的信息';
$curr\u user=wp\u get\u current\u user();
$user\u id=$curr\u user->id;
$curr\u username=$curr\u user->data->user\u login;
$body.='';
$body.='账户';
$body.='Username:'.$curr_Username';
$body.='First name:'。获取用户元($user\u id,'billing\u First\u name',true)。“”;
$body.=“姓氏:”。获取用户元($user\u id,'billing\u Last\u name',true)。“”;
$body.=“电话:”。获取字段(“用户电话”);
$body.=“年龄:”。获取字段(“用户年龄”);
$body.='';
//将内容类型设置为HTML
$headers=array('Content-Type:text/html;charset=UTF-8;');
}
}新的技术变更();
} 
遗憾的是,未显示自定义字段。我帐户中的所有字段都已填写,我收到一封关于更改用户数据的电子邮件。但自定义字段不会显示在电子邮件中

我尝试了以下选项:

$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>';
$body.='Phone:'。获取字段(“用户电话”、“用户1”);
$body.='电话:'。获取字段('user\u phone',$user\u id)。“”;

我很乐意接受您的帮助。

首先,您不能在逻辑上使用
wp\u get\u current\u user()
进行电子邮件通知或后端订单,因为不再有当前用户

但是如果您查看一下,您会注意到
$user\u id
作为钩子参数传递,您可以在相关函数中使用它,如下所示:

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(); 
} 
如果(!class_存在('wooCommerconTifyChanges')){
类别转换{
函数_u构造(){
//客户保存主帐户数据
添加操作('woocommerce\u save\u account\u details',数组($this,'woocommerce\u send\u notification'),15,1);
}
功能:发送通知($user\u id){
$body='';
$to$email@domain.com“;//将接收此电子邮件的地址
$subject='您的一位客户已更新了他们的信息';
$user=新的WP\u用户($user\u id);
$user\u name=$user->user\u login;
$body.='';
$body.=''。u uu(“账户”)。;
$body.='Username:'.$user_name';
$body.='First name:'。$user->billing_First_name';
$body.='Last name:'。$user->billing_Last_name';
$body.=“电话:”。获取_字段('user_Phone',$user_id)。“”;
$body.='Age:'。获取_字段('user_Age',$user_id)。“”;
$body.='';
//将内容类型设置为HTML
$headers=array('Content-Type:text/html;charset=UTF-8;');
}
} 
新的技术变更();
} 

现在,它应该可以工作了。

要从ACF中的用户记录中获取字段,您不仅要传递用户ID,还必须传递一个带有“user_u”前缀的字符串,因此字符串应该是:
“user_{$user_uid}”
,因此整个过程将是
获取字段('user_uage',“user_{$user_uid}”)


请参阅文档:

遗憾的是,未显示“用户电话”和“用户年龄”字段。这些自定义字段已创建并正确无误。它们显示在我的帐户中。在电子邮件中,没有。可能是什么问题?@dimitry,这取决于它们被激活的位置和保存方式。因此,您应该尝试用这些信息更新您的问题(可能是他们设置的一些屏幕截图)…我们无法猜测您正在做什么以及如何做,关于这两个ACF字段。请记住,“寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码”。以下是设置的屏幕截图:我创建了一个新字段。显示并保存此字段,但不显示电子邮件<代码>$body.='电话:'。获取字段('user\u new\u phone',$user\u id)。“”@Dimitry您应该使用此更新您的问题,而不是将其添加为注释。我无能为力,因为这与您自己的ACF设置和保存数据有关。用于保存ACF字段值的用户ID有问题。。。
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(); 
}