Woocommerce new customer administartion email noti

2019-06-11 14:39发布

I have tried to further develop the code posted by @danielevigi at Woocommerce New Customer Admin Notification Email to create an admin email when new customer register.

I wanted to add to the email not only the $user_login, but the fields that are present in the registration form as follows:

function new_customer_registered_send_email_admin($user_login) {
  ob_start();
  do_action('woocommerce_email_header', 'New customer registered');
  $email_header = ob_get_clean();
  ob_start();
  do_action('woocommerce_email_footer');
  $email_footer = ob_get_clean();

    $billing_first_name = '';
    $billing_last_name = '';
    $billing_phone = '';
    $w55 = '';

    $user_id = get_user_by('login', $user_login);
    if ($user_id)
    {
    $billing_first_name = get_user_meta($user_id, 'billing_first_name', true);
    $billing_last_name = get_user_meta($user_id, 'billing_last_name', true);
    $billing_phone = get_user_meta($user_id, 'billing_phone', true);
    $w55_card = get_user_meta($user_id, 'w55', true);
      if($w55_card === 'y')
      {
          $tessera = 'Voglio iscrivermi a Wision55';
      }
      else
      {
          $tessera = '';
      }
    }

  woocommerce_mail(
    get_bloginfo('admin_email'),
    get_bloginfo('name').' - New customer registered',
    $email_header.

    '
    <p>The user '.esc_html( $user_login ).' is registered to the website</p>' .
    '<p>Nome: <span style="font-size:16px; font-weight: bold;">' .$billing_first_name .'<span></p>
     <p>Cognome: <span style="font-size:16px; font-weight: bold;">' .$billing_last_name .'<span></p>
     <p>Telehone: <span style="font-size:16px; font-weight: bold;">' .$billing_phone .'<span></p>
     <p>Tessera W55: <span style="font-size:16px; font-weight: bold;">' .$tessera .'<span></p>'
    .$email_footer
  );
}
add_action('new_customer_registered', 'new_customer_registered_send_email_admin');

However the email is sent with the fields empty. I wonder what am I doing wrong... Anybody could help me?

2条回答
我只想做你的唯一
2楼-- · 2019-06-11 15:08

You cant retrieve data from the user with get_user_meta in this place, because the meta data for the user its not stored in the database yet, you can grab the data from the post request with $_POST, for example:

$_POST['billing_first_name'];

Maybe you can hook the send of the email in the woocommerce_created_customer hook, check this answer.

查看更多
三岁会撩人
3楼-- · 2019-06-11 15:11

When using get_user_by, the return is the WP_User object on success.

You can retrieve the user ID with

$user = get_user_by('login', $user_login);
$user_id = $user->ID;

You can read more about this function here

Hope it works

查看更多
登录 后发表回答