0

I'm using the following code the remove checkout fields when the Local pickup shipping method is selected.

The code work as espected for the native woocommerce billing fields but it doesn't remove the 'required' validation for the custom fields created from the WooCommerce Checkout Field Editor plugin.

It hides the custom fields but they stay required and the form can't submit.

When I select the Local pickup shipping method it removes the custom fields 'billing_region' and 'billing_cityalt' but when I press the checkout button it show an validation error that the fields are empty and need to be filled.

The question is how the make the custom fields not required?

I tried the ['required'] = false; and 'unset' but this approach doesn't work for custom fields.

add_filter( 'woocommerce_checkout_fields', 'hide_local_pickup_method' ); 
function hide_local_pickup_method( $fields_pickup ) {
    // change below for the method
    $shipping_method_pickup ='local_pickup:8';
    // change below for the list of fields. Add (or delete) the field name you want (or don’t want) to use
    $hide_fields_pickup = array( 'billing_address_1', 'billing_address_2','billing_region', 'billing_cityalt');
 
    $chosen_methods_pickup = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_pickup = $chosen_methods_pickup[0];
 
    foreach($hide_fields_pickup as $field_pickup ) {
        if ($chosen_shipping_pickup == $shipping_method_pickup) {
            $fields_pickup['billing'][$field_pickup]['required'] = false;
            $fields_pickup['billing'][$field_pickup]['class'][] = 'hide_pickup';
        }
        $fields_pickup['billing'][$field_pickup]['class'][] = 'billing-dynamic_pickup';
    }
    return $fields_pickup;
}
// Local Pickup - hide fields
add_action( 'wp_head', 'local_pickup_fields', 999 );
function local_pickup_fields() {
    if (is_checkout()) :
    ?>
    <style>
        .hide_pickup {display: none!important;}
    </style>
    <script>
        jQuery( function( $ ) {
            if ( typeof woocommerce_params === 'undefined' ) {
                return false;
            }
            $(document).on( 'change', '#shipping_method input[type="radio"]', function() {
                // change local_pickup:4 accordingly
            $('.billing-dynamic_pickup').toggleClass('hide_pickup', this.value == 'local_pickup:8');
            });
        });
    </script>
    <?php
    endif;
}  

1 Answer 1

0

I found a solution to the problem.

This code remove the validation of the custom fields if the Local Pickup method is selected.

add_action('woocommerce_after_checkout_validation', 'thwcfe_remove_unwanted_error', 11, 2);
function thwcfe_remove_unwanted_error($data, $errors){
    if (isset($data['shipping_method']) && $data['shipping_method'][0] == ' local_pickup:8') {
        $errors->remove(' billing_region');
        $errors->remove(' billing_cityalt');
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.