How can i integrate customer registration in account and on order page + extra fields?

Unfortunately, some functions that are standard in other store systems are still missing in WooCommerce. However, due to the scalability of the system, ways can be found to make these changes later.

The following code takes care of several things at once. Firstly, it can be used to implement a registration for the customer on the My Account login page, as well as on the order overview page. In addition, two extra fields are created within the registration, which are also visible to the admin in the backend and also displayed with each order mail to the admin. The code must be added in the function.php of the used theme, best at the end of the file.

// Extrafelder definieren
function woo_get_account_fields() {
      return apply_filters( 'woo_account_fields', array(
        'user_extrafeld1' => array(
          'type'        => 'text',
          'label'       => __( 'Extrafeld1', 'woocommerce' ),
          'placeholder' => __( '', 'woocommerce' ),
          'required'    => true,
          'sanitize'    => 'wc_clean',
          'description'       => 'Beschreibung Beispieltext',
          'hide_in_account'      => false,
          'hide_in_admin'        => false,
          'hide_in_checkout'     => false,
          'hide_in_registration' => false,
        ),
      
        'user_extrafeld2' => array(
          'type'        => 'text',
          'label'       => __( 'Extrafeld2', 'woocommerce' ),
          'placeholder' => __( '', 'woocommerce' ),
          'required'    => true,
          'sanitize' => 'wc_clean',
          'description'       => 'Beschreibung Beispieltext',
          'hide_in_account'      => false,
          'hide_in_admin'        => false,
          'hide_in_checkout'     => false,
          'hide_in_registration' => false,
        ),
      ) );
  }


  function woo_print_user_frontend_fields() {
      $fields            = woo_get_account_fields();
      $is_user_logged_in = is_user_logged_in();
   
      foreach ( $fields as $key => $field_args ) {
          $value = null;
   
          if ( $is_user_logged_in && ! empty( $field_args['hide_in_account'] ) ) {
              continue;
          }
   
          if ( ! $is_user_logged_in && ! empty( $field_args['hide_in_registration'] ) ) {
              continue;
          }
   
          if ( $is_user_logged_in ) {
              $user_id = woo_get_edit_user_id();
              $value   = woo_get_userdata( $user_id, $key );
          }
   
          $value = isset( $field_args['value'] ) ? $field_args['value'] : $value;
   
          woocommerce_form_field( $key, $field_args, $value );
      }
  }
  add_action( 'woocommerce_register_form', 'woo_print_user_frontend_fields', 10 );
  add_action( 'woocommerce_edit_account_form', 'woo_print_user_frontend_fields', 10 );


  function woo_checkout_fields( $checkout_fields ) {
      $fields = woo_get_account_fields();
   
      foreach ( $fields as $key => $field_args ) {
          if ( ! empty( $field_args['hide_in_checkout'] ) ) {
              continue;
          }
   
          $checkout_fields['account'][ $key ] = $field_args;
      }
   
      return $checkout_fields;
  }
  add_filter( 'woocommerce_checkout_fields', 'woo_checkout_fields', 10, 1 );


  function woo_print_user_admin_fields() {
      $fields = woo_get_account_fields();
      ?>
        <table class="form-table" id="woo-additional-information">
          <tbody>
          <?php foreach ( $fields as $key => $field_args ) { ?>
              <?php
              if ( ! empty( $field_args['hide_in_admin'] ) ) {
                  continue;
              }
   
              $user_id = woo_get_edit_user_id();
              $value   = woo_get_userdata( $user_id, $key );
              ?>
              <tr>
                  <th>
                      <label for="<?php echo $key; ?>"><?php echo $field_args['label']; ?></label>
                  </th>
                  <td>
                      <?php $field_args['label'] = false; ?>
                      <?php woocommerce_form_field( $key, $field_args, $value ); ?>
                  </td>
              </tr>
          <?php } ?>
          </tbody>
      </table>
      <?php
  }
  add_action( 'show_user_profile', 'woo_print_user_admin_fields', 30 );
  add_action( 'edit_user_profile', 'woo_print_user_admin_fields', 30 );


  function woo_get_edit_user_id() {
      return isset( $_GET['user_id'] ) ? (int) $_GET['user_id'] : get_current_user_id();
  }


  function woo_save_account_fields( $customer_id ) {
      $fields = woo_get_account_fields();
      $sanitized_data = array();
   
      foreach ( $fields as $key => $field_args ) {
          if ( ! woo_is_field_visible( $field_args ) ) {
              continue;
          }
   
          $sanitize = isset( $field_args['sanitize'] ) ? $field_args['sanitize'] : 'wc_clean';
          $value    = isset( $_POST[ $key ] ) ? call_user_func( $sanitize, $_POST[ $key ] ) : '';
   
          update_user_meta( $customer_id, $key, $value );
      }
   
      if ( ! empty( $sanitized_data ) ) {
          $sanitized_data['ID'] = $customer_id;
          wp_update_user( $sanitized_data );
      }
  }
  add_action( 'woocommerce_created_customer', 'woo_save_account_fields' );
  add_action( 'personal_options_update', 'woo_save_account_fields' );
  add_action( 'edit_user_profile_update', 'woo_save_account_fields' );
  add_action( 'woocommerce_save_account_details', 'woo_save_account_fields' );


  function woo_is_field_visible( $field_args ) {
      $visible = true;
      $action = filter_input( INPUT_POST, 'action' );
   
      if ( is_admin() && ! empty( $field_args['hide_in_admin'] ) ) {
          $visible = false;
      } elseif ( ( is_account_page() || $action === 'save_account_details' ) && is_user_logged_in() && ! empty( $field_args['hide_in_account'] ) ) {
          $visible = false;
      } elseif ( ( is_account_page() || $action === 'save_account_details' ) && ! is_user_logged_in() && ! empty( $field_args['hide_in_registration'] ) ) {
          $visible = false;
      } elseif ( is_checkout() && ! empty( $field_args['hide_in_checkout'] ) ) {
          $visible = false;
      }
   
      return $visible;
  }


  function woo_is_userdata( $key ) {
      $userdata = array(
          'user_pass',
          'user_login',
          'user_nicename',
          'user_email',
          'display_name',
          'nickname',
          'first_name',
          'last_name',
          'description',
          'rich_editing',
          'user_registered',
          'role',
          'jabber',
          'aim',
          'yim',
          'show_admin_bar_front',
    'user_extrafeld1',
    'user_extrafeld2',
      );
   
      return in_array( $key, $userdata );
  }


  function woo_validate_user_frontend_fields( $errors ) {
      $fields = woo_get_account_fields();
   
      foreach ( $fields as $key => $field_args ) {
          if ( empty( $field_args['required'] ) ) {
              continue;
          }
   
          if ( ! isset( $_POST['register'] ) && ! empty( $field_args['hide_in_account'] ) ) {
              continue;
          }
   
          if ( isset( $_POST['register'] ) && ! empty( $field_args['hide_in_registration'] ) ) {
              continue;
          }
   
          if ( empty( $_POST[ $key ] ) ) {
              $message = sprintf( __( '%s muss angegeben werden.', 'woo' ), '<strong>' . $field_args['label'] . '</strong>' );
              $errors->add( $key, $message );
          }
      }
   
      return $errors;
  }
  add_filter( 'woocommerce_registration_errors', 'woo_validate_user_frontend_fields', 10 );
  add_filter( 'woocommerce_save_account_details_errors', 'woo_validate_user_frontend_fields', 10 );


  function woo_add_post_data_to_account_fields( $fields ) {
      if ( empty( $_POST ) ) {
          return $fields;
      }
   
      foreach ( $fields as $key => $field_args ) {
          if ( empty( $_POST[ $key ] ) ) {
              $fields[ $key ]['value'] = '';
              continue;
          }
   
          $fields[ $key ]['value'] = $_POST[ $key ];
      }
   
      return $fields;
  }
  add_filter( 'woo_account_fields', 'woo_add_post_data_to_account_fields', 10, 1 );


  function woo_get_userdata( $user_id, $key ) {
      if ( ! woo_is_userdata( $key ) ) {
          return get_user_meta( $user_id, $key );
      }
   
      $userdata = get_userdata( $user_id );
   
      if ( ! $userdata || ! isset( $userdata->{$key} ) ) {
          return '';
      }
   
      return $userdata->{$key};
  }
  add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
  add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );


  function change_default_checkout_country() {
    return 'DE';
  }
  function change_default_checkout_state() {
    return 'DE';
  }


  add_action( 'woocommerce_register_form_start', 'woo_add_name_woo_account_registration' );
  function woo_add_name_woo_account_registration() {
      ?>
   
      <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>
    <?php
      $countries_obj   = new WC_Countries();
      $countries   = $countries_obj->get_allowed_countries();
      woocommerce_form_field('billing_country', array(
      'type'       => 'select',
      'class'      => array( 'chzn-drop' ),
      'label'      => __('Land'),
      'placeholder'    => __(''),
      'options'    => $countries,
      'required' => 1
      )
    );?>
    
    <p class="form-row">
      <label for="reg_billing_address_1"><?php _e( 'Straße & Hausnummer', 'woocommerce' ); ?> <span class="required">*</span></label>
      <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
      </p>
    
    <p class="form-row">
      <label for="reg_billing_postcode"><?php _e( 'Postleitzahl', 'woocommerce' ); ?> <span class="required">*</span></label>
      <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
      </p>
    
    <p class="form-row">
      <label for="reg_billing_city"><?php _e( 'Ort / Stadt', 'woocommerce' ); ?> <span class="required">*</span></label>
      <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
      </p>
    
    <p class="form-row">
      <label for="reg_billing_phone"><?php _e( 'Telefon', '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>
   
      <div class="clear"></div>
   
      <?php
  }
   

  add_filter( 'woocommerce_registration_errors', 'woo_validate_name_fields', 10, 3 );
  function woo_validate_name_fields( $errors, $username, $email ) {
      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
          $errors->add( 'billing_first_name_error', __( 'Vorname muss angegeben werden.', 'woocommerce' ) );
      }
      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
          $errors->add( 'billing_last_name_error', __( 'Nachname muss angegeben werden.', 'woocommerce' ) );
      }
    
     if ( isset( $_POST['billing_country'] ) && empty( $_POST['billing_country'] ) ) {
          $errors->add( 'billing_country_error', __( 'Land muss angegeben werden.', 'woocommerce' ) );
      }
    
     if ( isset( $_POST['billing_address_1'] ) && empty( $_POST['billing_address_1'] ) ) {
          $errors->add( 'billing_address_1_name_error', __( 'Straße & Hausnummer muss angegeben werden.', 'woocommerce' ) );
      }
    
     if ( isset( $_POST['billing_postcode'] ) && empty( $_POST['billing_postcode'] ) ) {
          $errors->add( 'billing_postcode_name_error', __( 'Postleitzahl muss angegeben werden.', 'woocommerce' ) );
      }
    
     if ( isset( $_POST['billing_city'] ) && empty( $_POST['billing_city'] ) ) {
          $errors->add( 'billing_city_name_error', __( 'Ort / Stadt muss angegeben werden.', 'woocommerce' ) );
      }
    
     if ( isset( $_POST['billing_phone'] ) && empty( $_POST['billing_phone'] ) ) {
          $errors->add( 'billing_phone_error', __( 'Telefon muss angegeben werden.', 'woocommerce' ) );
      }
    
      return $errors;
  }


  add_action( 'woocommerce_created_customer', 'woo_save_name_fields' );
  function woo_save_name_fields( $customer_id ) {
      if ( isset( $_POST['billing_first_name'] ) ) {
          update_user_meta( $customer_id, 'billing_first_name', sanitize_text_field( $_POST['billing_first_name'] ) );
          update_user_meta( $customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']) );
      }
      if ( isset( $_POST['billing_last_name'] ) ) {
          update_user_meta( $customer_id, 'billing_last_name', sanitize_text_field( $_POST['billing_last_name'] ) );
          update_user_meta( $customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']) );
      }
    if ( isset( $_POST['billing_country'] ) ) {
          update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
      }
    if ( isset( $_POST['billing_address_1'] ) ) {
          update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
      }
    if ( isset( $_POST['billing_postcode'] ) ) {
          update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
      }
    if ( isset( $_POST['billing_city'] ) ) {
          update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
      }
    if ( isset( $_POST['billing_phone'] ) ) {
          update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
      }
   
  }


  function woo_add_customer_to_email_subject( $subject, $order ) {
    $subject = $order->billing_first_name . ' ' . $order->billing_last_name . ' - ' . $order->get_order_number() . ' - ' . $order->get_date_created()->format ('d.m.Y');
    return $subject;
  }
  add_filter( 'woocommerce_email_subject_new_order', 'woo_add_customer_to_email_subject', 1, 2 );


  add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
  function my_woocommerce_email_order_meta_keys( $keys ) {
      $keys['Extrafeld111'] = '_user_extrafeld1';
      return $keys;    
  }


  add_filter( 'woocommerce_email_order_meta_fields', 'woocommerce_email_order_meta_fields_func', 10, 3 );
  function woocommerce_email_order_meta_fields_func( $fields, $sent_to_admin, $order ) {
    $fields['user_extrafeld1'] = array(
      'label' => __( 'Extrafeld1', 'woocommerce' ),
      'value' => wptexturize( get_post_meta( $order->id, 'user_extrafeld1', true ) )
    );
    $fields['user_extrafeld2'] = array(
      'label' => __( 'Extrafeld2', 'woocommerce' ),
      'value' => wptexturize( get_post_meta( $order->id, 'user_extrafeld2', true ) )
    );
    return $fields;
  }


  add_action( 'woocommerce_email_order_details', 'woo_add_transaction_id', 10, 4);
  function woo_add_transaction_id( $order, $sent_to_admin, $plain_text, $email ){
      $_output = null;
      
      if ($sent_to_admin) {
          $_user = $order->get_user();
          
          if($_user) {
              $_userId = $_user->ID;
              $_extrafeld1 = get_user_meta( $_userId, 'user_extrafeld1', true );
              $_extrafeld2 = get_user_meta( $_userId, 'user_extrafeld2', true ); 
              
              if($_extrafeld1 != '')
                  $_output = 'Extrafeld: ' . $_extrafeld1;
                  
              if($_extrafeld2 != '')
                  $_output .= '<br />Extrafeld: ' . $_extrafeld2;
          }
      }
      
      echo $_output;
  }
Without cookies
This website does not use cookies or tracking. More information can be found in the privacy policy.