如果用户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’没有显示。这些自定义字段已创建并正确无误。它们显示在my-account中。在电子邮件中,没有。
下面是ACF设置的屏幕截图
我已经写好了,但是字段没有出现:
get_field( 'user_phone', $user_id )
发布于 2018-09-10 08:30:25
我认为问题在于当您传递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}" );
https://stackoverflow.com/questions/51961628
复制相似问题