Skip to main content
Login Join
Snippet · PHP

Add a Custom GST Number Field in WooCommerce Checkout

Shared by Amit Dholiya · September 2, 2026 · @woocommerce_checkout_fields, woocommerce_checkout_processwoocommerce_checkout_update_order_meta, woocommerce_admin_order_data_after_billing_address

8 views
Back to Snippets

Let customers enter their GST number at checkout, with built-in format validation, so you can issue GST-compliant invoices for business customers — commonly needed for stores selling in India.

Steps

  1. Open your theme’s functions.php file.
  2. Add the code below.
  3. Save the file.
  4. Open the WooCommerce Checkout page.

Done — a new GST Number field will appear in the checkout form, and orders will be validated, saved, and shown in the admin order screen.

Done — a new GST Number field will appear in the checkout form, and orders will be validated, saved, and shown in the admin order screen.

// Add the field
function add_gst_number_field( $fields ) {
    $fields['billing']['gst_number'] = array(
        'type'        => 'text',
        'label'       => 'GST Number',
        'placeholder' => 'e.g. 22AAAAA0000A1Z5',
        'required'    => false,
        'class'       => array( 'form-row-wide' ),
        'priority'    => 30,
    );
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'add_gst_number_field' );

// Validate GST format at checkout
function validate_gst_number_field() {
    if ( ! empty( $_POST['gst_number'] ) ) {
        $gst = sanitize_text_field( $_POST['gst_number'] );
        if ( ! preg_match( '/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/', $gst ) ) {
            wc_add_notice( 'Please enter a valid GST number.', 'error' );
        }
    }
}
add_action( 'woocommerce_checkout_process', 'validate_gst_number_field' );

// Save it to the order (HPOS-safe)
function save_gst_number_field( $order_id ) {
    if ( ! empty( $_POST['gst_number'] ) ) {
        $order = wc_get_order( $order_id );
        $order->update_meta_data( 'GST Number', sanitize_text_field( $_POST['gst_number'] ) );
        $order->save();
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'save_gst_number_field' );

// Display it on the admin order edit page
function display_gst_number_admin( $order ) {
    $value = $order->get_meta( 'GST Number' );
    if ( $value ) {
        echo '<p><strong>GST Number:</strong> ' . esc_html( $value ) . '</p>';
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_gst_number_admin' );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments