I've added a custom field to the checkout page (billing_vat) and it needs to be required when the country is set to Ireland (IE).
Currently, I've changed the label to show that it's required in the same way as all the other fields using JavaScript and have hooked into 'woocommerce_get_country_locale' to change the field to required for IE.
add_filter('woocommerce_billing_fields', 'dc_custom_billing_fields', 1000, 1);
function dc_custom_billing_fields( $fields ) {
$fields['billing_vat'] = array(
'label' => 'VAT Number',
'required' => false,
'type' => 'text',
'class' => array( 'form-row-wide' ),
'priority' => 35,
);
return $fields;
}
add_filter( 'woocommerce_get_country_locale', 'dc_change_locale_field_defaults', 1000, 1 );
function dc_change_locale_field_defaults($countries) {
$countries['IE']['billing_vat']['required'] = true;
return $countries;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'dc_display_admin_order_meta', 10, 1 );
function dc_display_admin_order_meta($order) {
echo '<p><strong>'.__('Billing VAT').':</strong> ' . get_post_meta( $order->get_id(), '_billing_vat', true ) . '</p>';
}
add_action( 'woocommerce_after_order_notes', 'dc_after_checkout_field' );
function dc_after_checkout_field() {
?>
<script>
(function($) {
$(document).ready(function (){
$('#billing_country').on('change',function() {
if ($('#billing_country').val() == 'IE') {
// Required
$('#billing_vat').prop('required', true);
$('label[for="billing_vat"] .optional').remove();
$('label[for="billing_vat"]').append('<abbr class="required" title="required">*</abbr>');
} else {
$('#billing_vat').removeProp('required');
$('label[for="billing_vat"] .required').remove();
$('label[for="billing_vat"]').append('<span class="optional">(optional)</span>');
}
})
});
})(jQuery);
</script>
<?php
}
However when I submit the form, with the country set to Ireland and the field empty, Woo doesn't say that the field is required.