0

In Woocommerce checkout page there is a "ship-to-different-address" checkbox and I would like when it is checked, to hide order_comments field (Order notes). If the checkbox is unchecked back again, order_comments field should be visible (unhidden).

Is this possible in functions.php?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Koangi
  • 23
  • 3

1 Answers1

1

The following code will hide Order notes section field when "ship-to-different-address" checkbox is checked and vice versa:

add_action( 'wp_footer', 'checkout_custom_script_js');
function checkout_custom_script_js() {
    // Only on front-end and checkout page
    if( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script>
    jQuery(function($){
        $('form.checkout').on( 'change', '#ship-to-different-address-checkbox', function(){
            if( $(this).prop('checked') === true )
                $('#order_comments_field').hide(); // Show
            else
                $('#order_comments_field').show(); // Hide
        })
    });
    </script>
    <?php
    endif;
}

Code goes on function.php file of your active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi This is great and works, but only if "order_comments" is not a required field. My field is a required one. Thanks in advance. – Koangi Mar 20 '19 at 23:26
  • @Koangi For showing hiding fields in checkout, see [this related recent answer](https://stackoverflow.com/a/55209546/3730754), that will show you how it works in something a bit different… – LoicTheAztec Mar 20 '19 at 23:44