-1

How do I create a function (code-snippets) in order to unset or make checkout woocommerce field not required based on type of person field?

There are 2 customer options: Company or Person. If a person is selected, for instance, company's field still showing as something required.

www.riobaldo.com.br

I appreciate for any help..

thank you very much..

  • Please describe more as I am checking company files are show/hide according to the "Type of Person " dropdown change. so what is exactly want? – Rajeev Singh Sep 17 '21 at 04:25
  • Make the field optional not required. After that use js jquery to validate based on selection. Check this example - https://stackoverflow.com/questions/54927919/show-hide-custom-woocommerce-checkout-field-based-on-selected-payment-method – Snuffy Sep 17 '21 at 06:35
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 24 '21 at 08:51

1 Answers1

0

As per our understanding, you want to add/remove require validation from company input fields according to certain types(drop-down change).

Now you can do like this:

add_action( 'wp_footer', 'conditionally_remove_require_fields' );
function conditionally_remove_require_fields(){
    // Only on checkout page
    if ( is_checkout() && ! is_wc_endpoint_url() ) : ?>
    <script>
        jQuery(document).on('change','#billing_persontype', function(){
            var opt = jQuery(this).val();
            var selector = '#billing_company_field';
            if(opt == 2){
                jQuery(selector).removeAttr('required');
                jQuery(selector).removeClass("validate-required");
                jQuery(selector + ' label .required').hide();
            }else{
                jQuery(selector).attr('required',true);
                jQuery(selector).addClass("validate-required");
                jQuery(selector + ' label .required').show();
            }
        });
    </script>
    <?php
    endif;
}

You can do modifications according to opt value(1,2) accordingly.

Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23
  • Hi Rajeev, thanks fo your response. I figured it out by turning off the required field. its located at Panel > Appearance > Personalize > Woocommerce. But now I am having hard times to change first and last rows classes. For instance, billing_neighborhood first and billing_city last row. – Gerson Almeida Sep 21 '21 at 01:10