Show hide Order notes field based on a checkbox in Woocommerce checkout

杀马特。学长 韩版系。学妹 提交于 2019-12-13 03:48:27

问题


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?


回答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.



来源:https://stackoverflow.com/questions/55270713/show-hide-order-notes-field-based-on-a-checkbox-in-woocommerce-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!