Setting Required Field to False not working
-
Hello everyone,
I have two custom fields “Student Name” and “Student Grade” in my checkout form.
I want to set their validation based on a parameter. So for e.g if ?type=org exists, unset these fields and set the required to false.
As soon as I set it to required to false, the fields show up as “optional” on frontend- but still gives an error that they are required field when I try to checkout.
Here is my code: `add_filter( ‘woocommerce_checkout_fields’ , ‘custom_override_checkout_fields’, 100 );
function custom_override_checkout_fields( $fields ) {
$fields[‘billing’][‘billing_student_name’] = array(
‘label’ => __(‘Student Name’, ‘woocommerce’),
//’required’ => true,
‘class’ => array(‘form-row-first’),
‘clear’ => true,
‘priority’ => 25,
);$fields[‘billing’][‘billing_student_grade’] = array(
‘label’ => __(‘Student Grade’, ‘woocommerce’),
//’required’ => true,
‘class’ => array(‘form-row-last’),
‘clear’ => true,
‘priority’ => 30,
‘type’ => number
);unset( $fields[‘billing’][‘billing_company’] );
unset( $fields[‘order’][‘order_comments’] );
if(isset($_GET[‘type’])) {
$fields[‘billing’][‘billing_student_grade’][‘required’] = false;
$fields[‘billing’][‘billing_student_name’][‘required’] = false;
unset( $fields[‘billing’][‘billing_student_name’] );
unset( $fields[‘billing’][‘billing_student_grade’] );
} else {
$fields[‘billing’][‘billing_student_name’][‘required’] = true;
$fields[‘billing’][‘billing_student_grade’][‘required’] = true;
}
return $fields;
}
The topic ‘Setting Required Field to False not working’ is closed to new replies.