title: Sync additional Billing registration fields with default Wordpress fields in WooCommerce
body: <p>I Have added the below codes into Woocommerce user registration form to get the Billing Details on the registration page. </p>
  
  <p>Now what is happening when a new user register, <strong>the first and last name will get registered in the database of billing details &amp; as well as in the default wordpress user account</strong>. If the user update his first name &amp; last name on his account (wordpress user account), the same should update on the billing details.</p>
  
  <p>Woocommerce settings:</p>
  
  <p>Guest checkout is disabled.
  Checkout page user registration is enabled.
  Login Page Registration is enabled.
  Only Registered user can make purchases.</p>
  
  <ol>
  <li>This is the user registration form where I'm pulling these additional billing details from checkout process.</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/WZr0L.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WZr0L.jpg" alt="User Registration Form"></a></p>
  
  <ol start="2">
  <li>On "Account Details" I updated the "First Name", it worked here but i didn't get the same update on "Billing Details". I want the same "First Name" &amp; "Last Name" update on the "Billing Details" if a user update these 2 fields and his email address on his "Account Details".</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/k6nvi.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k6nvi.jpg" alt="Updates &quot;First Name&quot; &amp; &quot;Last Name&quot; on &quot;Account Details"></a></p>
  
  <ol start="2">
  <li>Now after updating the "First Name" &amp; "Last Name" on "Account Details", I came back to "Billing Details" but it is still displaying the old "First Name" &amp; "Last Name" which were used during registeration process. Also, I want to hide these 3 fields from Billing details, "First Name", "Last Name" &amp; "Email Address"- to not to confuse the registered users. I need these updates on "Billing Details" in database only because these information will be printed on the invoices &amp; Emails</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/omy60.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/omy60.jpg" alt="Scrrenshot of &quot;Billing Details&quot; After Updates on &quot;Account Details&quot;"></a></p>
  
  <p>The data will only sync/update if an administrator or store manager go to the user profile (from back-end) and manually press the "update" button then only it will take the effects. I want the data to sync/update automatically when a registered user made any changes from his account (front-end). </p>
  
  <p>Any help will be highly appreciated.</p>
  
  <p>Please check the below code:</p>
  
  <pre><code>// Custom function to display the Billing Address form to registration page
  add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
  function zk_add_billing_form_to_registration(){
      $checkout = WC()-&gt;checkout;
      foreach ( $checkout-&gt;get_checkout_fields( 'billing' ) as $key =&gt; $field ) :
          if($key!='billing_email')
              woocommerce_form_field( $key, $field, $checkout-&gt;get_value( $key ) );
      endforeach;
  }
  
  // Custom function to save Usermeta or Billing Address of registered user
  add_action('woocommerce_created_customer','zk_save_billing_address');
  function zk_save_billing_address($user_id){
      $address = $_POST;
      foreach ($address as $key =&gt; $field){
          // Only billing fields values
          if( strpos( $key, 'billing_' ) !== false ){
              // Condition to add firstname and last name to user meta table
              if($key == 'billing_first_name' || $key == 'billing_last_name'){
                  $new_key = str_replace( 'billing_', '', $key );
                  update_user_meta( $user_id, $new_key, $_POST[$key] );
              }
              update_user_meta( $user_id, $key, $_POST[$key] );
          }
      }
  }
  
  // Checking &amp; validation of the additional fields in registration form.
  add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
  function zk_validation_billing_address( $username, $email, $validation_errors ){
      foreach ($_POST as $key =&gt; $field) :
          // Validation: Required fields
          if( strpos( $key, 'billing_' ) !== false ){
              if($key == 'billing_country' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error',  __( 'Please select a country.', 'woocommerce' ));
              }
              if($key == 'billing_first_name' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
              }
              if($key == 'billing_last_name' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
              }
              if($key == 'billing_address_1' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
              }
              if($key == 'billing_city' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
              }
              if($key == 'billing_state' &amp;&amp; empty($field) ){
                  if(count( WC()-&gt;countries-&gt;get_states($_POST['billing_country']) ) &gt; 0)
                      $validation_errors-&gt;add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
              }
              if($key == 'billing_postcode' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
              }
              /*
              if($key == 'billing_email' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
              }
              */
              if($key == 'billing_phone' &amp;&amp; empty($field) ){
                  $validation_errors-&gt;add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
              }
  
          }
      endforeach;
  }
  
  add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
  function sv_required_billing_fields( $fields ) {
      $fields['billing_phone']['required'] = true;
      $fields['billing_city']['required'] = true;
      $fields['billing_country']['required'] = true;
      $fields['billing_address_1']['required'] = true;
      return $fields;
  }
  
  // Hidding some billing fields (Wordpress edit user pages)
  add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
  function user_profile_hide_some_fields_css( $user ){
      ?&gt;
      &lt;style&gt;
      .user-edit-php table#fieldset-billing tr:first-child,
      .user-edit-php table#fieldset-billing tr:nth-child(2),
      .user-edit-php table#fieldset-billing tr:last-child {
          display:none;
      }
      &lt;/style&gt;
      &lt;?php
  }
  
  // Sync hidden billing fields (Wordpress edit user pages)
  add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
  add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
  function sync_user_data_wp_and_billing_wc( $user_id )
  {
      if( ! empty($_POST['first_name']) ) {
          update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
      }
  
      if( ! empty($_POST['last_name']) ) {
          update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
      }
  
      if( ! empty($_POST['email']) ) {
          update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
      }
  }
  </code></pre>