Koangi
Koangi

Reputation: 23

Make Order notes field required only when it is not hidden in Woocommerce checkout

In Woocommerce checkout page, I have managed to show or hide "Order notes" field when "ship-to-different-address" checkbox is checked or unchecked, using "Show hide Order notes field based on a checkbox in Woocommerce checkout" answer code to one of my questions.

I would like "Order notes" field to be required only when it's visible.
How can I make this "Order notes" field required when it's visible (not hidden) and enable the validation for it?

Any help is welcome.

Upvotes: 0

Views: 1398

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253773

The following code will make "Order notes" field to be required only when it's not hidden and will enable in this case the field validation.

Note (mandatory): The "Order notes" field has to be optional (just as in default Woocommerce), without that the code will not work properly.

The complete code:

// Remove "(optional)" label on "Order notes" field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'order_comments' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// The jQuery script that show hide "Order notes" field
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_js_script', 10, 1 );
function custom_checkout_scripts_js_script( $checkout ) {
    $required = esc_html__( 'required', 'woocommerce' );

    ?>
    <script type="text/javascript">
    (function($){
        var required     = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
            notesField   = '#order_comments_field',
            shipCheckbox = '#ship-to-different-address-checkbox';

        function actionRequire( actionToDo='yes', selector='' ){
            if ( actionToDo == 'yes' ) {
                $(selector).addClass("validate-required");
                $(selector+' label').append(required);
            } else {
                $(selector).removeClass("validate-required");
                $(selector+' label > .required').remove();
            }
            $(selector).removeClass("woocommerce-validated");
            $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
        }

        // On load and on form change
        $('form.checkout').on( 'change', shipCheckbox, function(){
            if( $(this).prop('checked') === true ) {
                $(notesField).hide(); // Show
                actionRequire( 'no' ,notesField );
            } else {
                $(notesField).show(); // Hide
                actionRequire( 'yes' ,notesField );
            }
        });
    })(jQuery);
    </script>
    <?php
}

// "Order notes" field validation
add_action('woocommerce_checkout_process', 'order_comments_field_validation');
function order_comments_field_validation() {
    if ( isset($_POST['order_comments']) && empty($_POST['ship_to_different_address']) )
        wc_add_notice( __( 'Please fill in "Order notes" required field.' ), 'error' );
}

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


Similar: Make Woocommerce checkout phone field optional based on shipping country
Related: Show hide Order notes field based on a checkbox in Woocommerce checkout

Upvotes: 1

Related Questions