In the this example we will be adding a phone number field to the start of the form. It will be saved ad billing phone number.
The code goes in functions.php
file or in any other file that is connected to the functions.php
file.
Adding input field to form
add_action('woocommerce_register_form_start', function(){
woocommerce_form_field('billing_phone',[
'type'=> 'tel',
'required'=> true,
'label'=> 'Phone',
], ( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' ) );
});
Validate form input
add_action('woocommerce_register_post', function($username, $email, $errors){
if( empty($_POST['billing_phone']) ){
$errors->add('billing_phone_error', 'Please enter a Phone number.');
}
}, 10, 3);
Save form input value
add_action('woocommerce_created_customer', function($customer_id){
if( isset($_POST['billing_phone']) ){
update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']));
}
});
You can a more fields using this method and validate form values as per your need.