Back to Snippets

WooCommerce: Add Custom Checkout Field

PHP WooCommerce April 6, 2026

Add a validated custom checkout field to WooCommerce billing — includes field rendering, validation, and order meta storage.

Snippet Stats

Lines 35
Characters 1,225
Read 1 min
php • 35 lines
/**
 * Add a custom field to the checkout billing section.
 */
add_action( 'woocommerce_after_checkout_billing_form', function( $checkout ) {
    woocommerce_form_field( 'company_vat', array(
        'type'        => 'text',
        'class'       => array( 'form-row-wide' ),
        'label'       => __( 'VAT Number', 'theme-domain' ),
        'placeholder' => __( 'e.g. GB123456789', 'theme-domain' ),
        'required'    => false,
    ), $checkout->get_value( 'company_vat' ) );
} );

/**
 * Validate the custom field.
 */
add_action( 'woocommerce_checkout_process', function() {
    $vat = isset( $_POST['company_vat'] ) ? sanitize_text_field( wp_unslash( $_POST['company_vat'] ) ) : '';
    if ( $vat && ! preg_match( '/^[A-Z]{2}[0-9A-Z]{2,12}$/', strtoupper( $vat ) ) ) {
        wc_add_notice( __( 'Please enter a valid VAT number.', 'theme-domain' ), 'error' );
    }
} );

/**
 * Save the custom field to order meta.
 */
add_action( 'woocommerce_checkout_update_order_meta', function( $order_id ) {
    if ( ! empty( $_POST['company_vat'] ) ) {
        update_post_meta(
            $order_id,
            '_company_vat',
            sanitize_text_field( wp_unslash( $_POST['company_vat'] ) )
        );
    }
} );

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This React Higher Order Component handles authentication and authorization by restricting access to components based on user roles. It utilizes React ...

React Higher Order Component For Role-Based Authentication And Authorization

PHP
Apply automatic percentage discount for logged-in WooCommerce customers with a shop page notice. Handles admin and product type checks.

WooCommerce: Discount for Logged-In Users