Created
March 9, 2026 05:55
-
-
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.
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['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