WooCommerce: Add and Validate a Custom Checkout Field

Many Indian stores and donation websites need one extra detail at checkout, such as a PAN number for 80G receipts or a GST number for invoices. This tutorial adds an optional PAN field, validates its format, saves it to the order and shows it to the admin, using only WooCommerce hooks.

Step 1: Add the field

The woocommerce_checkout_fields filter lets you add, remove or reorder checkout fields. Adding the field to the billing group places it with the billing details:

add_filter( 'woocommerce_checkout_fields', function ( $fields ) {
    $fields['billing']['billing_pan'] = array(
        'type'        => 'text',
        'label'       => __( 'PAN (for 80G receipt)', 'my-plugin' ),
        'placeholder' => 'ABCDE1234F',
        'required'    => false,
        'class'       => array( 'form-row-wide' ),
        'priority'    => 120,
        'maxlength'   => 10,
    );
    return $fields;
} );

priority controls the position; the default billing fields use values from 10 to 110.

Step 2: Validate the value

A PAN has five letters, four digits and one letter. Validation runs when the customer places the order; an error notice stops the order until the value is fixed:

add_action( 'woocommerce_checkout_process', function () {
    $pan = isset( $_POST['billing_pan'] )
        ? strtoupper( sanitize_text_field( wp_unslash( $_POST['billing_pan'] ) ) )
        : '';

    if ( '' !== $pan && ! preg_match( '/^[A-Z]{5}[0-9]{4}[A-Z]$/', $pan ) ) {
        wc_add_notice( __( 'Please enter a valid PAN, for example ABCDE1234F.', 'my-plugin' ), 'error' );
    }
} );

Always sanitise $_POST data. WooCommerce verifies the checkout nonce before this hook runs.

Step 3: Save it and show it in the admin

Save the value to the order in upper case and display it under the billing address on the order screen:

add_action( 'woocommerce_checkout_create_order', function ( $order, $data ) {
    if ( ! empty( $data['billing_pan'] ) ) {
        $order->update_meta_data( '_billing_pan', strtoupper( $data['billing_pan'] ) );
    }
}, 10, 2 );

add_action( 'woocommerce_admin_order_data_after_billing_address', function ( $order ) {
    $pan = $order->get_meta( '_billing_pan' );
    if ( $pan ) {
        echo '<p><strong>' . esc_html__( 'PAN', 'my-plugin' ) . ':</strong> ' . esc_html( $pan ) . '</p>';
    }
} );

Using the order object keeps the code compatible with High-Performance Order Storage (HPOS).

Testing checklist

  • Place a test order with an empty PAN: it should go through, because the field is optional.
  • Try an invalid value such as 1234: you should see the error notice.
  • Place an order with a valid PAN and check it appears on the order screen in the admin.
  • If your checkout page uses the Checkout block, switch to the classic shortcode or use the Additional Checkout Fields API instead.

Frequently asked questions

Does this work with the WooCommerce Checkout block?

No. These hooks work with the classic checkout (the [woocommerce_checkout] shortcode). The Checkout block uses the Additional Checkout Fields API, registered with woocommerce_register_additional_checkout_field().

Is this code compatible with HPOS?

Yes. It reads and writes order data only through the order object ($order->update_meta_data() and $order->get_meta()), never with direct post meta functions, so it works with High-Performance Order Storage.

Where should I put this code?

In a small custom plugin or a child theme functions.php file, never in the parent theme, where updates would remove it.

Need help with your website?

Tell me what you need and receive a free, page-by-page quotation.

Copyright © 2026 Himanshu Bisoyi All Rights Reserved.

Designed by Himanshu