PHPwoocommerceintermediate

Add Custom WooCommerce Checkout Field

Add a custom field to WooCommerce checkout page and save it to order meta

#woocommerce#checkout#custom-field#order-meta
Share this snippet:

Code

php
1// Add custom field to checkout
2function add_custom_checkout_field($checkout) {
3 echo '<div id="custom_checkout_field"><h3>' . __('Additional Information') . '</h3>';
4
5 woocommerce_form_field('gift_message', array(
6 'type' => 'textarea',
7 'class' => array('form-row-wide'),
8 'label' => __('Gift Message (Optional)'),
9 'placeholder' => __('Enter your gift message here...'),
10 'required' => false,
11 'rows' => 4,
12 ), $checkout->get_value('gift_message'));
13
14 echo '</div>';
15}
16add_action('woocommerce_after_order_notes', 'add_custom_checkout_field');
17
18// Validate the field (if required)
19function validate_custom_checkout_field() {
20 if (isset($_POST['gift_message']) && empty($_POST['gift_message'])) {
21 // Uncomment below to make it required
22 // wc_add_notice(__('Please enter a gift message.'), 'error');
23 }
24}
25add_action('woocommerce_checkout_process', 'validate_custom_checkout_field');
26
27// Save the custom field to order meta
28function save_custom_checkout_field($order_id) {
29 if (!empty($_POST['gift_message'])) {
30 update_post_meta($order_id, '_gift_message', sanitize_textarea_field($_POST['gift_message']));
31 }
32}
33add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_field');
34
35// Display custom field in order details (Admin)
36function display_custom_field_in_admin_order($order) {
37 $gift_message = get_post_meta($order->get_id(), '_gift_message', true);
38
39 if ($gift_message) {
40 echo '<div class="order_data_column">';
41 echo '<h3>' . __('Gift Message') . '</h3>';
42 echo '<p><strong>' . esc_html($gift_message) . '</strong></p>';
43 echo '</div>';
44 }
45}
46add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_field_in_admin_order');
47
48// Display custom field in order emails
49function add_custom_field_to_order_emails($order, $sent_to_admin, $plain_text, $email) {
50 $gift_message = get_post_meta($order->get_id(), '_gift_message', true);
51
52 if ($gift_message) {
53 if ($plain_text) {
54 echo "\n" . __('Gift Message:') . "\n" . $gift_message . "\n";
55 } else {
56 echo '<h2>' . __('Gift Message') . '</h2>';
57 echo '<p>' . esc_html($gift_message) . '</p>';
58 }
59 }
60}
61add_action('woocommerce_email_order_meta', 'add_custom_field_to_order_emails', 10, 4);
62
63// Display custom field on thank you page
64function display_custom_field_on_thank_you_page($order_id) {
65 $gift_message = get_post_meta($order_id, '_gift_message', true);
66
67 if ($gift_message) {
68 echo '<section class="woocommerce-gift-message">';
69 echo '<h2>' . __('Your Gift Message') . '</h2>';
70 echo '<p>' . esc_html($gift_message) . '</p>';
71 echo '</section>';
72 }
73}
74add_action('woocommerce_thankyou', 'display_custom_field_on_thank_you_page');

Add Custom WooCommerce Checkout Field

This snippet adds a custom field to the WooCommerce checkout page, validates it, saves it to order meta, and displays it in order details.

// Add custom field to checkout
function add_custom_checkout_field($checkout) {
    echo '<div id="custom_checkout_field"><h3>' . __('Additional Information') . '</h3>';

    woocommerce_form_field('gift_message', array(
        'type'          => 'textarea',
        'class'         => array('form-row-wide'),
        'label'         => __('Gift Message (Optional)'),
        'placeholder'   => __('Enter your gift message here...'),
        'required'      => false,
        'rows'          => 4,
    ), $checkout->get_value('gift_message'));

    echo '</div>';
}
add_action('woocommerce_after_order_notes', 'add_custom_checkout_field');

// Validate the field (if required)
function validate_custom_checkout_field() {
    if (isset($_POST['gift_message']) && empty($_POST['gift_message'])) {
        // Uncomment below to make it required
        // wc_add_notice(__('Please enter a gift message.'), 'error');
    }
}
add_action('woocommerce_checkout_process', 'validate_custom_checkout_field');

// Save the custom field to order meta
function save_custom_checkout_field($order_id) {
    if (!empty($_POST['gift_message'])) {
        update_post_meta($order_id, '_gift_message', sanitize_textarea_field($_POST['gift_message']));
    }
}
add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_field');

// Display custom field in order details (Admin)
function display_custom_field_in_admin_order($order) {
    $gift_message = get_post_meta($order->get_id(), '_gift_message', true);

    if ($gift_message) {
        echo '<div class="order_data_column">';
        echo '<h3>' . __('Gift Message') . '</h3>';
        echo '<p><strong>' . esc_html($gift_message) . '</strong></p>';
        echo '</div>';
    }
}
add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_field_in_admin_order');

// Display custom field in order emails
function add_custom_field_to_order_emails($order, $sent_to_admin, $plain_text, $email) {
    $gift_message = get_post_meta($order->get_id(), '_gift_message', true);

    if ($gift_message) {
        if ($plain_text) {
            echo "\n" . __('Gift Message:') . "\n" . $gift_message . "\n";
        } else {
            echo '<h2>' . __('Gift Message') . '</h2>';
            echo '<p>' . esc_html($gift_message) . '</p>';
        }
    }
}
add_action('woocommerce_email_order_meta', 'add_custom_field_to_order_emails', 10, 4);

// Display custom field on thank you page
function display_custom_field_on_thank_you_page($order_id) {
    $gift_message = get_post_meta($order_id, '_gift_message', true);

    if ($gift_message) {
        echo '<section class="woocommerce-gift-message">';
        echo '<h2>' . __('Your Gift Message') . '</h2>';
        echo '<p>' . esc_html($gift_message) . '</p>';
        echo '</section>';
    }
}
add_action('woocommerce_thankyou', 'display_custom_field_on_thank_you_page');

Field Types Available

You can use different field types by changing the type parameter:

  • text: Single line text input
  • textarea: Multi-line text area
  • select: Dropdown select
  • checkbox: Single checkbox
  • radio: Radio buttons
  • password: Password field
  • email: Email input with validation
  • tel: Phone number input
  • number: Numeric input

Example: Dropdown Field

woocommerce_form_field('delivery_time', array(
    'type'     => 'select',
    'label'    => __('Preferred Delivery Time'),
    'required' => true,
    'options'  => array(
        ''           => __('Select...'),
        'morning'    => __('Morning (8am - 12pm)'),
        'afternoon'  => __('Afternoon (12pm - 5pm)'),
        'evening'    => __('Evening (5pm - 8pm)'),
    ),
), $checkout->get_value('delivery_time'));

Dependencies

  • WooCommerce

Related Snippets