Woocommerce validation field is required only if visible

I am trying to make the business field in a woocommerce request necessary, but only if the field is actually displayed.

I created a switch box with woocommerce_form_field that displays two options. using javascript, I switch the visibility of the business input field when checking the switches:

jQuery(document).ready(function () { $('input[type=radio][name=customer_type]').on('change', function () { $('.checkout-bedrijfsnaam').slideToggle(); }); }); 

default css to enter "business":

 .checkout-bedrijfsnaam { display:none; } 

In addition, I made the "business" field, which must be used with Woocommerce "woocommerce_billing_fields" and "woocommerce_shipping_fields":

 $address_fields['billing_company']['required'] = true; 

that everything is going fine, however: When the "business" field is turned off and not displayed, I get a "field required" warning and cannot send the form. Is it possible to make this field necessary, but only if it is actually displayed?

I know what this should be, since the fields in shipping_fields files behave the same (not required when the checkbox is unchecked), but I cannot get around it.

+6
source share
1 answer

If the check is performed only on the client side, instead of hiding it with display: none; , you can remove it from the DOM. When you need it, you will add it again. You already have a switch for this.

0
source

All Articles