Last active
March 18, 2026 08:15
-
-
Save pramodjodhani/bafe97fab2ee61c366ca3132d8e27577 to your computer and use it in GitHub Desktop.
Prevent Flux Stepper from advancing to the next step if EU VAT number is invalid (BTW number validation)
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
| <?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['billing_vat_number'] ) && ! empty( $fields['billing_vat_number']['value'] ) ) { | |
| $vat_value = $fields['billing_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['billing_vat_number'] ) || ! is_array( $messages['billing_vat_number'] ) ) { | |
| $messages['billing_vat_number'] = array(); | |
| } | |
| // Set the custom error flag & message to block the stepper | |
| $messages['billing_vat_number']['message'] = __( 'You must enter a valid EU VAT number.', 'woocommerce' ); | |
| $messages['billing_vat_number']['isCustom'] = true; | |
| } | |
| } | |
| return $messages; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment