Created
May 26, 2025 11:13
-
-
Save pramodjodhani/c7d2faca1d9aacf1e011f60cc9d96e97 to your computer and use it in GitHub Desktop.
Flux checkout - custom validatio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('woocommerce_checkout_fields', 'add_custom_checkout_field'); | |
function add_custom_checkout_field($fields) { | |
$fields['billing']['billing_custom_field'] = array( | |
'type' => 'text', | |
'label' => __('BTW-nummer'), | |
'required' => false, // has to be false, otherwise it's always checked dispite of country selected! | |
'class' => array('form-row-wide', 'tax-number-class'), | |
'label_class' => array('tax-number-label'), | |
'clear' => true, | |
); | |
return $fields; | |
} | |
function flux_checkout_custom_inline_error_message( $messages ) { | |
$value = $_POST['fields']['billing_custom_field']['value'] ?? false; | |
$is_present = isset( $_POST['fields']['billing_custom_field']['value'] ) ? true : false; | |
$country = $_POST['fields']['billing_country']['value'] ?? false; | |
if ( $country !== 'BE' ) { | |
$messages['billing_custom_field']['message'] = 'Please enter the value.'; | |
$messages['billing_custom_field']['isCustom'] = false; | |
return $messages; | |
} | |
if ( $is_present && ! $value ) { | |
$messages['billing_custom_field']['message'] = 'Please enter the value.'; | |
$messages['billing_custom_field']['isCustom'] = true; | |
return $messages; | |
} | |
if ( ! $value ) { | |
return $messages; | |
} | |
if ($value == '') { | |
return $messages; | |
} | |
if ( preg_match( '/^\d{10}$/', $value ) ) { | |
return $messages; | |
} | |
$messages['billing_custom_field']['message'] = 'BTW-nummer moet exact 10 cijfers bevatten.'; | |
$messages['billing_custom_field']['isCustom'] = true; | |
return $messages; | |
} | |
add_filter( 'flux_checkout_check_for_inline_errors', 'flux_checkout_custom_inline_error_message' ); | |
// Add JavaScript to show/hide BTW-nummer field based on country selection | |
add_action('wp_footer', 'hide_btw_nummer_for_non_belgium'); | |
function hide_btw_nummer_for_non_belgium() { | |
if (!is_checkout()) return; | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($) { | |
// Function to check if Belgium is selected and show/hide the BTW field | |
function checkCountry() { | |
var country = $('#billing_country').val(); | |
if (country === 'BE') { | |
$('.tax-number-class').show(); | |
} else { | |
$('.tax-number-class').hide(); | |
$('#billing_custom_field').val(''); // Clear the field when hidden | |
$('#billing_custom_field').closest('.form-row').removeClass('woocommerce-invalid validate-required') | |
} | |
} | |
// Run on page load | |
checkCountry(); | |
// Run when country is changed | |
$(document).on('change', '#billing_country', function() { | |
checkCountry(); | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment