Hello @hadimargo,
Copy the below code and insert in your theme’s functions.php. Alternatively you can use plugin like “Code snippet” in order to add it safely.
The below code add a new phone number field on the WooCommerce registration form. The data will be saved as a billing phone number.
function wooc_extra_register_fields() {?>
<p class="form-row form-row-wide">
<input type="text" class="input-text" name="billing_phone" placeholder="Phone number" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
/**
* register fields Validating.
*/
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['reg_billing_phone'] ) && empty( $_POST['reg_billing_phone'] ) ) {
$validation_errors->add( 'reg_billing_phone_error', __( '<strong>Error</strong>: Phone number is required!', 'woocommerce' ) );
}
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
/**
* Below code save extra fields.
*/
function wooc_save_extra_register_fields( $customer_id ) {
if ( isset( $_POST['billing_phone'] ) ) {
// Phone input filed which is used in WooCommerce
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
Hope this helps!
Naz.