Why when changing field required parameter to false field is still required?

I need to dinamicly change city field from required to non-required with ajax in my woocommerce checkout.

Ajax callback function:

Read More
function my_action_callback(){
    WC()->checkout->checkout_fields['billing']['billing_city']['required'] = false;
    $fase = WC()->checkout->checkout_fields;
    wp_send_json($fase);
}
add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

Ajax function:

$(document).on('change', '#shipping_method_0', function(){
    var v = $(this).val();
    $.post(ajaxurl, {
            'action': 'my_action',
            'data': {'val':v}
        }, function(response){
            console.log(response);
        }
    );
});

and devtool console screenshot

screensot

And after that when i submit the checkout form this field is still required by form response, even if this field is removed from html with devtool.

Related posts

1 comment

  1. It’s still being required within wc’s php. Add a filter disabling this requirement.

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
    
    function custom_override_checkout_fields( $fields ) {
         $fields['order']['billing_city']['required'] = false;
         return $fields;
    }
    

Comments are closed.