php - Switch required/unrequired on Woocommerce Checkout Fields
one text
I am trying to add or remove some checkout fields when an user click a radio button or an other. I am creating two event listeners and trying to make some ajax request to change the status of the checkout filed - from required false to true.
This is my code:
jQuery(".radio_fizica").click(function() {
jQuery('.factura_firma').hide();
});
jQuery(".radio_juridica").click(function() {
jQuery('.factura_firma').slideDown();
ajaxurl = '<?php echo(admin_url('admin-ajax.php')); ?>';
jQuery.ajax({
url: ajaxurl,
data: {
'action':'example_ajax_request',
},
success:function(data) {
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
And this is the php code in functions.php
function sv_unrequire_wc_phone_field( $fields ) {
$fields['billing_company']['required'] = true;
$fields['billing_company_code']['required'] = true;
$fields['billing_company_nr']['required'] = true;
return $fields;
}
function example_ajax_request( ) {
if ( isset($_REQUEST) ) {
add_filter( 'woocommerce_billing_fields', 'sv_unrequire_wc_phone_field');
}
die();
}
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action('wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
Somehow, i can't get the fields to be required.
Source