PHPwoocommerceintermediate
Add Custom WooCommerce Checkout Field
Add a custom field to WooCommerce checkout page and save it to order meta
Faisal Yaqoob
January 9, 2025
#woocommerce#checkout#custom-field#order-meta
Code
php
1 // Add custom field to checkout 2 function 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 } 16 add_action('woocommerce_after_order_notes', 'add_custom_checkout_field'); 17
18 // Validate the field (if required) 19 function 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 } 25 add_action('woocommerce_checkout_process', 'validate_custom_checkout_field'); 26
27 // Save the custom field to order meta 28 function 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 } 33 add_action('woocommerce_checkout_update_order_meta', 'save_custom_checkout_field'); 34
35 // Display custom field in order details (Admin) 36 function 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 } 46 add_action('woocommerce_admin_order_data_after_billing_address', 'display_custom_field_in_admin_order'); 47
48 // Display custom field in order emails 49 function 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 } 61 add_action('woocommerce_email_order_meta', 'add_custom_field_to_order_emails', 10, 4); 62
63 // Display custom field on thank you page 64 function 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 } 74 add_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
WooCommerce Custom Checkout Fields
Add custom fields to WooCommerce checkout with validation and order display
PHPwoocommerceintermediate
phpPreview
// Add custom fields to checkout page
add_action('woocommerce_after_order_notes', 'custom_checkout_fields');
function custom_checkout_fields($checkout) {
echo '<div id="custom_checkout_fields"><h3>' . __('Additional Information') . '</h3>';
...#woocommerce#checkout#custom-fields+2
12/23/2025
View
WooCommerce Bulk Discounts and Quantity Pricing
Implement bulk discounts, quantity-based pricing, and tiered discounts in WooCommerce
PHPwoocommerceadvanced
phpPreview
// Apply bulk discount based on quantity
add_action('woocommerce_before_calculate_totals', 'apply_bulk_discount');
function apply_bulk_discount($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
...#woocommerce#pricing#discounts+2
12/23/2025
View
WooCommerce Custom Email Notifications
Create custom email notifications for WooCommerce events and triggers
PHPwoocommerceadvanced
phpPreview
// Create custom email class
if (!class_exists('WC_Custom_Order_Email')) {
class WC_Custom_Order_Email extends WC_Email {
...#woocommerce#email#notifications+2
12/23/2025
View