Add a custom field to the WooCommerce checkout page

Add a custom field to the WooCommerce checkout page

The WooCommerce checkout collects the usual data: billing and shipping address, contact details, payment method. Real shops often need more at the moment of purchase: a gift message, a preferred delivery time, artwork instructions, a company VAT number. The checkout is the right place for it, because the data belongs to that particular order.

Problem: The WooCommerce checkout does not have a field you need, for example a gift message.

Solution: Three hooks make the full cycle: output the field, validate it, save it to the order. Two more show the value in the admin and in the emails.

The code

In functions.php:

<?php
// 1. Output the field after the order notes
add_action( 'woocommerce_after_order_notes', 'ha_add_gift_message_field' );

function ha_add_gift_message_field( $checkout ) {
    woocommerce_form_field( 'ha_gift_message', array(
        'type'        => 'textarea',
        'class'       => array( 'form-row-wide' ),
        'label'       => __( 'Gift message (optional)', 'theme-name' ),
        'placeholder' => __( 'Write a personal message for the recipient...', 'theme-name' ),
        'required'    => false,
    ), $checkout->get_value( 'ha_gift_message' ) );
}

// 2. Validate on submit
add_action( 'woocommerce_checkout_process', 'ha_validate_gift_message_field' );

function ha_validate_gift_message_field() {
    if ( isset( $_POST['ha_gift_message'] ) && mb_strlen( wp_unslash( $_POST['ha_gift_message'] ) ) > 300 ) {
        wc_add_notice( __( 'The gift message must be at most 300 characters.', 'theme-name' ), 'error' );
    }
}

// 3. Save to the order
add_action( 'woocommerce_checkout_create_order', 'ha_save_gift_message_field' );

function ha_save_gift_message_field( $order ) {
    if ( ! empty( $_POST['ha_gift_message'] ) ) {
        $order->update_meta_data( '_ha_gift_message', sanitize_textarea_field( wp_unslash( $_POST['ha_gift_message'] ) ) );
    }
}

woocommerce_form_field() outputs the field with the same markup and classes as the default checkout fields, so it matches the theme. A notice added with wc_add_notice( ..., 'error' ) in the validation stops the order and shows the message above the form.

The value is saved in woocommerce_checkout_create_order through the order object, right before the order is written, so no separate save is needed. The underscore at the start of the meta key hides it from the Custom Fields box.

Show it in the admin and in emails

<?php
// In the order screen, under the billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', function ( $order ) {
    $message = $order->get_meta( '_ha_gift_message' );
    if ( $message ) {
        echo '<p><strong>' . esc_html__( 'Gift message', 'theme-name' ) . ':</strong> ' . nl2br( esc_html( $message ) ) . '</p>';
    }
} );

// In the order emails
add_filter( 'woocommerce_email_order_meta_fields', function ( $fields, $sent_to_admin, $order ) {
    $message = $order->get_meta( '_ha_gift_message' );
    if ( $message ) {
        $fields['ha_gift_message'] = array( 'label' => __( 'Gift message', 'theme-name' ), 'value' => $message );
    }
    return $fields;
}, 10, 3 );

NOTE: Read and write order data through the order object ($order->get_meta(), $order->update_meta_data()), not with get_post_meta() on the order ID. It works the same today and keeps the code working if the orders are moved out of the posts table, which WooCommerce later did with High-Performance Order Storage. For a collection of other useful snippets see useful WooCommerce functions.