0

I'm trying to create a function that sends an email to the admin when a new user registers. I would like the email to contain the users first name, last name, email address, and billing information added via WooCommerce. This is what I have so far, but for some reason $id and $email are the only variables that show up in the email, but nothing else.. What am I doing wrong? lol

function admin_notification($user_id) { 
    $user = get_userdata( $user_id );
    $id = $user->id;
    $email = $user->user_email;
    $firstname = $user->first_name;
    $lastname = $user->last_name;
    $phone = get_user_meta($id, 'billing_phone', true);
    $address = get_user_meta($id, 'billing_address_1', true);
    $city = get_user_meta($id, 'billing_city', true);
    $state = get_user_meta($id, 'billing_state', true);
    $zip = get_user_meta($id, 'billing_postcode', true);

    $headers = array('Content-Type: text/html; charset=UTF-8','From: My WordPress Site <noreply@example.com>');
    $message = '<p>'. $firstname .' '. $lastname .' has requested registration to the website. Please verify their account as soon as possible.</p><hr /><p><strong>First Name:</strong> '.$firstname.'<br><strong>Last Name:</strong> '.$lastname.'<br><strong>Email Address:</strong> '.$email.'<br><strong>Phone Number:</strong> '.$phone.'<br><strong>Address:</strong><br>'.$address.', '.$city.' '.$state.' '.$zip.'</p><br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><table border="0" cellspacing="0" cellpadding="0"><tr><td bgcolor="green" style="padding: 12px 18px 12px 18px; -webkit-border-radius:3px; border-radius:3px" align="center"><a href="https://example.com/wp-admin/user-edit.php?user_id='.$id.'" target="_blank" style="font-size: 16px; font-family: Helvetica, Arial, sans-serif; font-weight: normal; color: #ffffff; text-decoration: none; display: inline-block;">Verify User</a></td></tr></table></td></tr></table>';

    wp_mail('admin@example.com', 'New Customer Registration', $message, $headers);
}

add_action('user_register', 'admin_notification', 10, 1); 
  • $user instance doesn't have a property id, use ID instead. $id = $user->ID;. – Shazzad Dec 20 '19 at 12:25
0

I'm not sure how WooCommerce adds these fields, but I suspect it uses add_user_meta after the user_register form or something. Try and catch it this way instead:

if ( isset( $_POST['first_name'] ) ) {
    $first_name = $_POST['first_name'];
}
0

Maybe try using the wp_new_user_notification() function instead. That function is what gets triggered when a new user signs up. My feeling is that all the data you need is going to be available to send in a new message. There's a solid example in the link I posted. I hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.