Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
Is it possible to disable validation for some checkout fields dynamically with JavaScript?

I have created a few checkout fields with similar data and they are required.

add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields', 50);
function custom_woocommerce_billing_fields($fields) {
    $fields['billing']['billing_company'] = array(
        'label' => 'Company', 
        'type' => 'text',
        'class'        => array( 'form-row-first' ),
        'required' => true,
    );
    $fields['billing']['billing_OIB'] = array(
        'label'        => 'OIB',
        'type'        => 'text',
        'class'        => array( 'form-row-last' ),
        'required'     => true,
    );
}

A customer has a radio button option to choose if he is buying as a private person or on behalf of a company. If he is buying for private purposes, then with JavaScript I will hide some of unneeded fields like company name and I also remove class name validate-required. If he is buying for a company, then I hide first name, last name etc. This code is bigger but I think you get the idea.

    notNeededField.style.display = 'none';
    notNeededField.classList.remove('validate-required');

But this doesn't work completely because WooCommerce probably does additional validation of each field in PHP. So even after I want to checkout as a private person, Woocommerce still gives me message that company name is required.

Is it possible to turn off/on validation on the fly with JavaScript and how? Alternatively, what would be the best solution? Maybe to set required parameter on those fields as false and only do custom JavaScript validation?

about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda