I have added extra registration fields to the WooCommerce registration form. Among these fields are fields for state and country.
This is how I get the state field in the form:
$field = [
'type' => 'state',
'label' => 'State',
'required' => 1,
'class' => ['address-field form-row-first'],
'validate' => ['state']
];
woocommerce_form_field( 'billing_state', $field, '' );
The validation for the state field is done using the following snippet:
if (isset($_POST['billing_state']) && empty($_POST['billing_state']) ) {
$validation_errors->add('billing_state_error', __('Please enter your State', 'woocommerce'));
}
The validation works fine but I noticed that when a country like Netherlands is chosen, the state field disappears since Netherlands does not have any states. This is ok but when I click on the Sign Up button, the validation for state still runs even though the field is no longer there.
Would anyone know what change I will need to make in order to skip the validation for a field if it is hidden? Thank you.
2
Answers
The following would do it:
Reference: See this file.
Well in this case you need ti check if that field is hidden or not
Then remove required from that field
In your case you also need