I'm trying to add some custom fields in the Woocommerce User account registration form.
I've succesfully added First Name and last name, nut i'm trying to add a T&C agreement checkbox, that if it isn't selected it doesn't let the user continue the registration.
I've managed to display it correctly the i cannot make the validation work.
What am i doing wrong?
PS. just to know, is it possibile to make these checks live, without needing to reload the page?
Thanks
The code i've used:
add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );
function cbi_custom_woo_account_registration() {
?>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" value="<?php if ( ! empty( $_POST['accept_terms'] ) ) esc_attr_e( $_POST['accept_terms'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );
function cbi_validate_custom_reg_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
}
if ( isset($_POST['accept_terms']) && $_POST['accept_terms'] == 0 ) {
$errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
}
return $errors;
}
I have fixed your code.
add_action( 'woocommerce_register_form_start', 'cbi_custom_woo_account_registration' );
function cbi_custom_woo_account_registration() {
?>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide first">
<label for="reg_billing_first_name"><?php _e( 'First name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="reg_billing_last_name"><?php _e( 'Last name', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST['billing_last_name'] ) ) esc_attr_e( $_POST['billing_last_name'] ); ?>" />
</p>
<p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
<label for="accept_terms"><?php _e( 'I agree TeC', 'custom-cbi-parts' ); ?> <span class="required">*</span></label>
<input type="checkbox" class="woocommerce-Input woocommerce-Input--text input-text form-control" name="accept_terms" id="accept_terms" <?php if( isset($_POST['accept_terms'])){ echo "checked"; } ?> />
</p>
<div class="clear"></div>
<?php
}
// 2. VALIDATE FIELDS
add_action( 'woocommerce_registration_errors', 'cbi_validate_custom_reg_fields', 10, 3 );
function cbi_validate_custom_reg_fields( $errors, $username, $email ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', __( 'First name is required!', 'custom-cbi-parts' ) );
}
if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', __( 'Last name is required!.', 'custom-cbi-parts' ) );
}
if ( !isset($_POST['accept_terms']) ) {
$errors->add( 'accept_terms_error', __( 'You must accept Terms and Conditions!.', 'custom-cbi-parts' ) );
}
return $errors;
}