Custom fields in the WooCommerce Checkout block
In the classic checkout, extra fields were added with the woocommerce_checkout_fields filter. The Checkout block ignores that filter: it is a React application that talks to the Store API. Since WooCommerce 8.9 (2024), the Additional Checkout Fields API registers fields in PHP that the block renders, validates and saves by itself, without any JavaScript in the plugin.
Problem: A shop switched to the Checkout block, and its VAT number and delivery note fields disappeared. The old code used woocommerce_checkout_fields and woocommerce_checkout_update_order_meta, which the block does not call.
Solution: Register the fields with woocommerce_register_additional_checkout_field() on woocommerce_init, choose the location (contact, address or order), add sanitizing and validation callbacks, and read the values from the order with the CheckoutFields service or the generated meta keys.
Register the fields
add_action( 'woocommerce_init', function (): void {
if ( ! function_exists( 'woocommerce_register_additional_checkout_field' ) ) {
return;
}
woocommerce_register_additional_checkout_field( array(
'id' => 'ha/vat-number',
'label' => __( 'VAT number', 'textdomain' ),
'optionalLabel' => __( 'VAT number (optional)', 'textdomain' ),
'location' => 'address',
'type' => 'text',
'required' => false,
'attributes' => array( 'autocomplete' => 'off', 'pattern' => '[A-Z]{2}[A-Za-z0-9]{2,12}' ),
'sanitize_callback' => fn( $value ) => strtoupper( preg_replace( '/\s+/', '', (string) $value ) ),
'validate_callback' => function ( $value ) {
if ( '' !== $value && ! preg_match( '/^[A-Z]{2}[A-Z0-9]{2,12}$/', $value ) ) {
return new WP_Error( 'ha_invalid_vat', __( 'Please enter a valid VAT number.', 'textdomain' ) );
}
},
) );
woocommerce_register_additional_checkout_field( array(
'id' => 'ha/delivery-note',
'label' => __( 'Note for the courier', 'textdomain' ),
'location' => 'order',
'type' => 'text',
) );
} );
- The ID must have a namespace (
ha/) to avoid collisions. location:contact(with the email, saved to the customer account too),address(shown in both billing and shipping address forms, saved per address),order(an extra block in the order section).- Types:
text,select(withoptions) andcheckbox. - The sanitize callback runs first, then the validation; a returned
WP_Erroris shown at the field and stops the checkout.
Read the values
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
add_action( 'woocommerce_order_status_processing', function ( int $order_id ): void {
$order = wc_get_order( $order_id );
$fields = Package::container()->get( CheckoutFields::class );
$vat = $fields->get_field_from_object( 'ha/vat-number', $order, 'billing' );
$note = $fields->get_field_from_object( 'ha/delivery-note', $order, 'other' );
if ( $vat ) {
ha_erp_set_vat( $order_id, $vat );
}
} );
The values are stored as order meta (with keys like _wc_billing/ha/vat-number and _wc_other/ha/delivery-note), so they work with HPOS and appear automatically in the order admin, the customer’s account, and the order emails. Reading them through the service avoids depending on the key format.
Fields that only appear sometimes
Conditions such as “show the VAT field only for company customers” can be expressed with required and hidden rules in JSON schema format (added to the API in 2025, check the WooCommerce version of the shop), evaluated in the browser and on the server:
// part of the array passed to woocommerce_register_additional_checkout_field()
$ha_vat_field['hidden'] = array(
'customer' => array(
'properties' => array(
'address' => array(
'properties' => array( 'company' => array( 'const' => '' ) ),
),
),
),
);
The field is hidden while the company field is empty.
Classic checkout
The API does not add the fields to the shortcode checkout. A shop that still uses [woocommerce_checkout] keeps the classic hooks; a plugin that supports both registers the fields twice, once per system. Hooks around the order itself: WooCommerce order status transitions.
NOTE: The fields are part of the Store API checkout request, so they also work in headless frontends that use the Store API: the WooCommerce REST API for a headless storefront. Only these three field types exist; for a date picker or file upload, an Inner Block extension of the Checkout block is still needed.