0

I've been tasked with adding some fields to the Wordpress Registration Form.

See code below:

function wooc_extra_register_fields() {?>
       <p class="form-row form-row-first">
       <label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_company" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
       </p>
       <p class="form-row form-row-last">
       <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
       </p>
       <p class="form-row form-row-first">
       <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
       </p>
       <p class="form-row form-row-last">
       <label for="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
       </p>
       <p class="form-row form-row-wide">
       <label for="reg_billing_website"><?php _e( 'Website', 'woocommerce' ); ?><span class="required">*</span></label>
       <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_website'] ) ) esc_attr_e( $_POST['billing_website'] ); ?>" />
       </p>


       <div class="clear"></div>
       <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );



function custom_new_user_notification($user_id, $notify = '') {
    $user = get_userdata($user_id);
    $customer = get_userdata($user_id);
    
    get_bloginfo('name')
    
    $billing_first_name = $customer->get_billing_first_name();
    $billing_last_name  = $customer->get_billing_last_name();
    $billing_company    = $customer->get_billing_company();

    // Send email to user with login information
    wp_new_user_notification($user_id, $notify);

    // Send email to site administrator
    $to = 'user@domain.com'; // replace with your own email address
    $subject = 'New user registration';
    $message = 'A new user has registered on your site. User name: '.$user->user_login.', Email: '.$user->user_email.', First Name: ' . $billing_first_name . ', Last Name: ' . $billing_last_name . ', Company: ' . $billing_company . ' .';
    $headers = 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>' . "\r\n";
    wp_mail($to, $subject, $message, $headers);
}
add_action('user_register', 'custom_new_user_notification', 10, 1);

So on this line:

<br>
$message = 'A new user has registered on your site. User name: '.$user->user_login.', Email: '.$user->user_email.', First Name: ' . $billing_first_name . ', Last Name: ' . $billing_last_name . ', Company: ' . $billing_company . ' .';

"First Name: ' . $billing_first_name . '," - this doesn't seem to work.

"Last Name: ' . $billing_last_name . ', " - this doesn't seem to work.

"Company: ' . $billing_company . ' .' " - this doesn't seem to work.

Any idea why these fields don't show up? I've tested loads, same result.

ADyson
  • 57,178
  • 14
  • 51
  • 63
Paul M
  • 115
  • 12
  • Thankyou for the edits ADyson - appreciate it :) – Paul M Feb 21 '23 at 12:39
  • Doesn't work in what sense? You mean you just get a blank space where the text should be? Or the email no longer sends at all, or something? Have you debugged the code to see if `$billing_first_name` is even populated from the call to `$customer->get_billing_first_name();`? Are there any PHP errors or warnings in your logs (or on the screen)? – ADyson Feb 21 '23 at 12:39
  • I just get a blank space, but I'll try your edits now. – Paul M Feb 21 '23 at 12:39
  • Does this answer your question? [How can I get customer details from an order in WooCommerce?](https://stackoverflow.com/questions/22843504/how-can-i-get-customer-details-from-an-order-in-woocommerce) – DarkBee Feb 21 '23 at 12:49

1 Answers1

1

Use WC_Customer class object so we can get woocommerce data...

function custom_new_user_notification($user_id, $notify = '') {
    $user = get_userdata($user_id);
    $customer = new WC_Customer( $user_id );
    
    get_bloginfo('name')
    
    $billing_first_name = $customer->get_billing_first_name();
    $billing_last_name  = $customer->get_billing_last_name();
    $billing_company    = $customer->get_billing_company();

    // Send email to user with login information
    wp_new_user_notification($user_id, $notify);

    // Send email to site administrator
    $to = 'user@domain.com'; // replace with your own email address
    $subject = 'New user registration';
    $message = 'A new user has registered on your site. User name: '.$user->user_login.', Email: '.$user->user_email.', First Name: ' . $billing_first_name . ', Last Name: ' . $billing_last_name . ', Company: ' . $billing_company . ' .';
    $headers = 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>' . "\r\n";
    wp_mail($to, $subject, $message, $headers);
}
add_action('user_register', 'custom_new_user_notification', 10, 1);
DarkBee
  • 16,592
  • 6
  • 46
  • 58