问题
If the user Woocommerce changes his data, a mail to the administrator receives a email about these changes.
I need to show in this letter the custom fields that I created in the ACF. (In ACF created a questionnaire and put it into a /edit-account)
Here is the complete code, based on - Send notification email when customer changes address 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' ));
}
function woocommerce_send_notification(){
$body = '';
$to = 'email@domain.com'; //address that will receive this email
$subject = 'One of your customers has updated their information.';
$curr_user = wp_get_current_user();
$user_id = $curr_user->ID;
$curr_username = $curr_user->data->user_login;
$body .= '<table>';
$body .= '<tr><td><strong>Account</strong></td></tr>';
$body .= '<tr><td>Username: </td><td>' . $curr_username . '</td></tr>';
$body .= '<tr><td>First name: </td><td>' . get_user_meta( $user_id, 'billing_first_name', true ). '</td></tr>';
$body .= '<tr><td>Last name: </td><td>' . get_user_meta( $user_id, 'billing_last_name', true ) . '</td></tr>';
$body .= '<tr><td>Phone: </td><td>' . get_field('user_phone') . '</td></tr>';
$body .= '<tr><td>Age: </td><td>' . get_field('user_age') . '</td></tr>';
$body .= '</table>';
//set content type as HTML
$headers = array('Content-Type: text/html; charset=UTF-8;');
}
} new WooCommerceNotifyChanges();
}
Unfortunately, the custom field is not displayed. All fields in my account have been filled in, I received a email about changing the user's data. But custom fields are not shown in the email.
I tried the option:
$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>';
I will be happy with your help.
回答1:
First, you can't use logically wp_get_current_user()
for email notifications or backend orders, as there is no a current user anymore.
But if you have a look to woocommerce_save_account_details hook source code you will notice that $user_id
is passed as a hook argument and you can use it in the related function, as following:
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();
}
Now it should work.
回答2:
To get fields from a user record in ACF you don't just pass the user's ID, you have to pass it in a string prepended with "user_", so the string should be: "user_{$user_id}"
and the whole thing would therefore be get_field( 'user_age', "user_{$user_id}" )
See the docs: https://www.advancedcustomfields.com/resources/how-to-get-values-from-a-user/
来源:https://stackoverflow.com/questions/51715532/get-acf-custom-fields-values-in-woocommerce-custom-email-notification