I need to dinamicly change city field from required to non-required with ajax in my woocommerce checkout.
Ajax callback function:
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
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.
It’s still being required within wc’s php. Add a filter disabling this requirement.