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.