Skip to content

Instantly share code, notes, and snippets.

@pramodjodhani
Created March 9, 2026 05:55
Show Gist options
  • Select an option

  • Save pramodjodhani/781b3c51b787f8713b75cc8c60e1ab2b to your computer and use it in GitHub Desktop.

Select an option

Save pramodjodhani/781b3c51b787f8713b75cc8c60e1ab2b to your computer and use it in GitHub Desktop.
Prevent Flux Stepper from advancing to the next step if EU VAT number is invalid.
<?php
/**
* Prevent Flux Stepper from advancing to the next step if EU VAT number is invalid.
*/
add_filter( 'flux_checkout_check_for_inline_errors', 'flux_validate_eu_vat_number', 10, 1 );
function flux_validate_eu_vat_number( $messages ) {
// Check if fields were sent in the AJAX request
if ( ! isset( $_POST['fields'] ) || ! is_array( $_POST['fields'] ) ) {
return $messages;
}
// Retrieve the fields posted during the 'Next step' AJAX request
$fields = wc_clean( wp_unslash( $_POST['fields'] ) );
// Ensure our target field is present in the current step payload
if ( isset( $fields['woocommerce_eu_vat_number'] ) ) {
$vat_value = $fields['woocommerce_eu_vat_number']['value'];
// Remove spaces, dashes, and dots (matching your JS: .replace(/[\s\-\.]/g, '') )
$vat_cleaned = preg_replace( '/[\s\-\.]/', '', $vat_value );
// EU VAT Regex pattern
$pattern = '/^(AT|BE|BG|CY|CZ|DE|DK|EE|EL|ES|FI|FR|HR|HU|IE|IT|LT|LU|LV|MT|NL|PL|PT|RO|SE|SI|SK|XI)[A-Z0-9]{2,13}$/i';
// Check if the value is empty or fails the regex check
if ( empty( $vat_value ) || ! preg_match( $pattern, $vat_cleaned ) ) {
// Guarantee an array structure exists for this field
if ( ! isset( $messages['woocommerce_eu_vat_number'] ) || ! is_array( $messages['woocommerce_eu_vat_number'] ) ) {
$messages['woocommerce_eu_vat_number'] = array();
}
// Set the custom error flag & message to block the stepper
$messages['woocommerce_eu_vat_number']['message'] = __( 'You must enter a valid EU VAT number.', 'woocommerce' );
$messages['woocommerce_eu_vat_number']['isCustom'] = true;
}
}
return $messages;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment