title: How to save WooCommerce checkout custom fields to user meta
body: <p>I've created the following custom fields for the user meta using the <a href="http://www.pods.io" rel="nofollow">PODS.io</a> Wordpress plugin:</p>
  
  <ul>
  <li>date_of_birth</li>
  <li>emergency_contact_name</li>
  <li>relation</li>
  <li>emergency_phone</li>
  </ul>
  
  <p>I've added these fields to the WooCommerce checkout, under extra information, by using the following code in my theme's <code>functions.php</code>:</p>
  
  <pre><code>add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
  
  function my_custom_checkout_field( $checkout ) {
  
      woocommerce_form_field( 'date_of_birth', array(
          'type'          =&gt; 'text',
          'class'         =&gt; array('my-field-class form-row-wide'),
          'label'         =&gt; __('Date of Birth'),
          'placeholder'   =&gt; __('dd/mm/yyyy'),
          ), $checkout-&gt;get_value( 'date_of_birth' ));
  
      woocommerce_form_field( 'emergency_contact_name', array(
          'type'          =&gt; 'text',
          'class'         =&gt; array('my-field-class form-row-wide'),
          'label'         =&gt; __('Emergency Contact Name'),
          'placeholder'   =&gt; __('contact name'),
          ), $checkout-&gt;get_value( 'emergency_contact_name' ));
  
      woocommerce_form_field( 'relation', array(
          'type'          =&gt; 'text',
          'class'         =&gt; array('my-field-class form-row-wide'),
          'label'         =&gt; __('Emergency Relation'),
          'placeholder'   =&gt; __('wife/husband'),
          ), $checkout-&gt;get_value( 'relation' ));
  
      woocommerce_form_field( 'emergency_phone', array(
          'type'          =&gt; 'text',
          'class'         =&gt; array('my-field-class form-row-wide'),
          'label'         =&gt; __('Emergency Phone'),
          'placeholder'   =&gt; __('xxxx xxx xxx / xxxx xxxx'),
          ), $checkout-&gt;get_value( 'emergency_phone' ));
  
  }
  </code></pre>
  
  <p>Error checking:</p>
  
  <pre><code>add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
  
  function my_custom_checkout_field_process() {
      // Check if set, if its not set add an error.
      if ( ! $_POST['date_of_birth'] )
          wc_add_notice( __( 'Please enter your date of birth' ), 'error' );
      if ( ! $_POST['emergency_contact_name'] )
          wc_add_notice( __( 'Please enter your Emergency Contact Name' ), 'error' );     
      if ( ! $_POST['relation'] )
          wc_add_notice( __( 'Please enter how your Emergency Contact is related to you' ), 'error' );
      if ( ! $_POST['emergency_phone'] )
          wc_add_notice( __( 'Please enter the phone number of your Emergency Contact' ), 'error' );              
  }
  </code></pre>
  
  <p>(Hopefully) update the <strong>user meta</strong> upon checkout:</p>
  
  <pre><code>add_action( 'woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta' );
  
  function my_custom_checkout_field_update_order_meta( $order_id ) {
      if ( !empty( $_POST['date_of_birth'] ) ) {
          $dob = sanitize_text_field( $_POST['date_of_birth'] );
          update_user_meta( $current_user-&gt;ID, 'date_of_birth', $dob);
      }
      if ( ! empty( $_POST['emergency_contact_name'] ) ) {
          update_user_meta( $user_id, 'emergency_contact_name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
      }
      if ( ! empty( $_POST['relation'] ) ) {
          update_user_meta( $user_id, 'relation', sanitize_text_field( $_POST['relation'] ) );
      }
      if ( ! empty( $_POST['emergency_phone'] ) ) {
          update_user_meta( $user_id, 'emergency_phone', sanitize_text_field( $_POST['emergency_phone'] ) );
      }           
  }
  </code></pre>
  
  <p>Unfortunately, the user meta custom fields are not updated when I checkout.</p>
  
  <p>I can update the <strong>order meta</strong> custom fields with the following code:</p>
  
  <pre><code>add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
  
  function my_custom_checkout_field_update_order_meta( $order_id ) {
      if ( ! empty( $_POST['date_of_birth'] ) ) {
          update_post_meta( $order_id, 'Date Of Birth', sanitize_text_field( $_POST['date_of_birth'] ) );
      }
      if ( ! empty( $_POST['emergency_contact_name'] ) ) {
          update_post_meta( $order_id, 'Emergency Contact Name', sanitize_text_field( $_POST['emergency_contact_name'] ) );
      }
      if ( ! empty( $_POST['relation'] ) ) {
          update_post_meta( $order_id, 'Emergency Relation', sanitize_text_field( $_POST['relation'] ) );
      }
      if ( ! empty( $_POST['emergency_phone'] ) ) {
          update_post_meta( $order_id, 'Emergency Phone', sanitize_text_field( $_POST['emergency_phone'] ) );
      }           
  }
  </code></pre>
  
  <p>However, we need the custom fields in the user meta, not the order meta.</p>
  
  <p><strong>Can you see what is wrong with the code that saves custom fields upon checkout to the user meta?</strong></p>
  
  <p>Thanks.</p>