Save WooCommerce checkout custom field as user meta data

I want to add my custom registration field to my checkout page form.

I’m using this code for adding custom field to my registration area.

By the way, i’m using my checkout fields in to my registration fields.

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    $fields['billing']['shipping_tc'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );
    
    return $fields;
}

And I tried this code for update user meta

add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );
function reigel_woocommerce_checkout_update_user_meta( $customer_id, $posted ) {
    if (isset($posted['shipping_tc'])) {
        $dob = sanitize_text_field( $posted['shipping_tc'] );
        update_user_meta( $user_id, $dob, $_POST[$dob]);
    }
}

No errors but it doesn’t work… Anyone can help me?

I’m succesfully updating other default checkout values with help this code;

// 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 => $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] );
        }
    }
}

What can I do for update custom checkout fields by the registration?

Here is my registration page.

Answer

The main error is to use a checkout field which field key is starting with shipping_ in the billing section…

Also you should better use the hook the composite hook woocommerce_billing_fields which will do everything for you (so no need to save the field as user meta data or order item meta data as it’s done by WooCommerce).

So the only required code replacement will be (with field key billing_identifier instead of confusing shipping_tc):

add_filter( 'woocommerce_billing_fields' , 'add_custom_billing_field' );
function add_custom_billing_field( $fields ) {
    $fields['billing_identifier'] = array(
        'label' => __('TC Kimlik No', 'woocommerce'),
        'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
        'required' => true,
        'class' => array('form-row-wide'),
        'clear' => true
    );

    return $fields;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

The field will additionally appear in My account > Addresses > Edit Billing address.