# VAT Number Field on the Block Checkout

> Adds an optional VAT number field to the contact section of the Checkout block and rejects numbers that do not look like a VAT number.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, block-checkout, vat, b2b
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-block-checkout-vat-number/

## Code

```php
add_action( 'woocommerce_init', function () {
    if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
        return;
    }

    woocommerce_register_additional_checkout_field( array(
        'id'       => 'wpide/vat-number',
        'label'    => 'VAT number (optional)',
        'location' => 'contact',
        'type'     => 'text',
        'required' => false,
    ) );
} );

add_action( 'woocommerce_validate_additional_field', function ( $errors, $field_key, $field_value ) {
    if ( 'wpide/vat-number' !== $field_key || '' === trim( (string) $field_value ) ) {
        return;
    }

    $vat = strtoupper( preg_replace( '/[\s.\-]/', '', (string) $field_value ) );

    if ( ! preg_match( '/^(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|GB|CH|NO)[A-Z0-9+*]{8,12}$/', $vat ) ) {
        $errors->add( 'invalid_vat_number', 'Please enter a VAT number with its country prefix, for example DE123456789.' );
    }
}, 10, 3 );
```

## Notes

Needs WooCommerce 8.9 or newer; the check covers the format (country prefix plus 8 to 12 characters) and does not contact VIES.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "VAT Number Field on the Block Checkout" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
