1
votes

I use conditional checkout fields as given at Conditionally unset checkout field in woocommerce . But, It doesn't remove required validation fields? How can I pass conditional statement within "if (true)" to remove required validation ? At the other words, how can I check which option is selected? Regards

 if( true ){ // pass conditional statement here
     unset($fields['billing']['add_house_name']);  //  remove field
     $fields['billing']['add_building_name']['required']   = false; //            remove required validation
        }                   
        return $fields;
1
You can use condition something like this: if($fields['billing']['add_house_name'] ==true) { unset($fields['billing']['add_house_name']); // remove field $fields['billing']['add_building_name']['required'] = false; // } - Mohsin khan

1 Answers

1
votes

You can override checkout fields using this code:

// Hook in
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );

// Our hooked in function - $address_fields is passed via the filter!
function custom_override_default_address_fields( $address_fields ) {
     $address_fields['address_1']['required'] = false;

     return $address_fields;
}

You can add this hook to a condition where you check inputs based on which you want to trigger validation.