Reputation: 414
//Add phone number field to customer registration
add_filter( 'register_form', 'custom_phone_no_registration_field' );
function custom_phone_no_registration_field( ) {
echo '<div class="form-row form-row-wide"><label for="reg_phone">'.__('Phone Number', 'woocommerce').' <span class="required">*</span></label>
<input type="text" class="input-text" name="phone" id="reg_phone" size="30" value="'.esc_attr($_POST['phone']).'" /></div>';
}
//Validation registration form after submission using the filter registration_errors
add_action('registration_errors', 'custom_registration_errors_validation', 10,3);
function custom_registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
global $woocommerce;
extract($_POST); // extracting $_POST into separate variables
if(empty($phone)) :
$woocommerce->add_error( __( 'Please, fill in all the required fields.', 'woocommerce' ) );
endif;
return $reg_errors;
}
I'm using register_form function to add a phone number field to registration form.it works but the registration_errors function is not working. Can anyone please help me with the code ?
Upvotes: 1
Views: 3082
Reputation: 26319
I think you need to validate on the woocommerce_process_registration_errors
filter. Take a look at process_registration()
method in includes/class-wc-form-handler.php
.
add_action( 'woocommerce_process_registration_errors', 'custom_registration_error' );
function custom_registration_errors( $validation_error ){
if ( ! isset( $_POST['phone'] ) ){
$validation_error = new WP_Error( 'phone-error', __( 'Please enter a phone number.', 'your-plugin' ) );
}
return $validation_error;
}
Upvotes: 2