【发布时间】: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”字段未显示。这些自定义字段已创建并正确。它们显示在我的帐户中。在电子邮件中,没有。
我还是这么写的,但是字段没有出现:
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