Admin Order Fields Display
March 21, 2025 · AI Conversations
function custom_flower_delivery_fields($checkout) {
echo '' . __('Gift Delivery Details') . '';
woocommerce_form_field('gift_eircode', array(
'type' => 'text',
'class' => array('form-row-wide'),
'label' => __('Eircode'),
'required' => true,
), $checkout->get_value('gift_eircode'));
woocommerce_form_field('gift_phone', array(
'type' => 'tel',
'class' => array('form-row-wide'),
'label' => __('Phone Number'),
'required' => true,
), $checkout->get_value('gift_phone'));
}
add_action('woocommerce_checkout_process', 'custom_flower_delivery_field_validation');
function custom_flower_delivery_field_validation() {
if (empty($_POST['gift_eircode'])) {
wc_add_notice(__('Please enter the recipient Eircode.'), 'error');
}
if (empty($_POST['gift_phone'])) {
wc_add_notice(__('Please enter the recipient phone number.'), 'error');
}
}
add_action('woocommerce_checkout_create_order', 'custom_flower_delivery_field_save', 20, 2);
function custom_flower_delivery_field_save($order, $data) {
if (!empty($_POST['gift_eircode'])) {
$order->update_meta_data('gift_eircode', sanitize_text_field($_POST['gift_eircode']));
}
if (!empty($_POST['gift_phone'])) {
$order->update_meta_data('gift_phone', sanitize_text_field($_POST['gift_phone']));
}
}
add_action('woocommerce_admin_order_data_after_billing_address', 'display_gift_delivery_fields_admin', 10, 1);
function display_gift_delivery_fields_admin($order) {
echo '' . __('Recipient Eircode') . ': ' . get_post_meta($order->get_id(), 'gift_eircode', true) . '';
echo '' . __('Recipient Phone') . ': ' . get_post_meta($order->get_id(), 'gift_phone', true) . '';
}
add_filter('woocommerce_email_order_meta_fields', 'add_gift_delivery_fields_to_emails');
function add_gift_delivery_fields_to_emails($fields) {
$fields['gift_eircode'] = __('Recipient Eircode');
$fields['gift_phone'] = __('Recipient Phone Number');
return $fields;
}
display custom field in admin order detail page
If you'd like to **display them in a dedicated section** on the admin order edit screen (not just after the billing address), you can enhance the UX by placing the fields in their own panel like this:
—
### ✅ **1. Add a new panel in the admin order details page**
Replace or supplement this:
add_action('woocommerce_admin_order_data_after_billing_address', 'display_gift_delivery_fields_admin', 10, 1);
With this:
add_action('woocommerce_admin_order_data_after_order_details', 'display_gift_delivery_fields_admin_panel');
function display_gift_delivery_fields_admin_panel($order) {
$gift_eircode = get_post_meta($order->get_id(), 'gift_eircode', true);
$gift_phone = get_post_meta($order->get_id(), 'gift_phone', true);
echo '<div class="order_data_column">';
echo '<h4>' . __('Gift Delivery Details') . '</h4>';
echo '<p><strong>' . __('Recipient Eircode') . ':</strong> ' . esc_html($gift_eircode) . '</p>';
echo '<p><strong>' . __('Recipient Phone') . ':</strong> ' . esc_html($gift_phone) . '</p>';
echo '</div>';
}
—
### ✅ **2. Result**
This will show the "Gift Delivery Details" in its **own section** below the order details in the admin area, making it cleaner and easier to manage for store admins.
Let me know if you'd also like to make these fields editable in the admin, or display them on the customer’s My Account order view too.
To **display these custom fields on the WooCommerce admin order edit screen**, WooCommerce recommends using the `woocommerce_admin_order_data_after_order_details` action — which you’re now planning to use instead of `woocommerce_admin_order_data_after_billing_address`.
Here’s a refined version tailored for **`wc_orders_meta`**:
—
### ✅ **Final code to display from `wc_orders_meta` properly in admin**
add_action('woocommerce_admin_order_data_after_order_details', 'display_gift_delivery_fields_admin_panel');
function display_gift_delivery_fields_admin_panel($order) {
// Retrieve from WC_Order object
$gift_eircode = $order->get_meta('gift_eircode');
$gift_phone = $order->get_meta('gift_phone');
echo '<div class="order_data_column">';
echo '<h4>' . __('Gift Delivery Details') . '</h4>';
if ($gift_eircode) {
echo '<p><strong>' . __('Recipient Eircode') . ':</strong> ' . esc_html($gift_eircode) . '</p>';
}
if ($gift_phone) {
echo '<p><strong>' . __('Recipient Phone') . ':</strong> ' . esc_html($gift_phone) . '</p>';
}
echo '</div>';
}
—
### 🧠 Notes
– `get_post_meta()` is not needed if you're using `$order->update_meta_data()` and accessing data via `WC_Order->get_meta()`. WooCommerce encourages this approach now.
– This will show the info under the "Order Details" section (in its own column/panel).
– These fields are **not editable** from the admin by default. If you want to **make them editable** on the order page, I can help you with that as well.
Let me know if you’d like:
– Frontend "My Account > Orders" display
– Admin editing ability
– Export to CSV or reports inclusion
Here’s how you can properly include the **`gift_eircode`** and **`gift_phone`** fields in **customer and admin WooCommerce emails**:
—
### ✅ **1. Show custom fields in the WooCommerce order emails**
Use the `woocommerce_email_order_details` hook (preferred for inserting custom content into the email body, below the order table):
add_action('woocommerce_email_order_details', 'add_gift_delivery_fields_to_emails', 20, 4);
function add_gift_delivery_fields_to_emails($order, $sent_to_admin, $plain_text, $email) {
$gift_eircode = $order->get_meta('gift_eircode');
$gift_phone = $order->get_meta('gift_phone');
if ($gift_eircode || $gift_phone) {
if ($plain_text) {
echo "n" . __('Gift Delivery Details') . "n";
if ($gift_eircode) {
echo __('Recipient Eircode') . ': ' . $gift_eircode . "n";
}
if ($gift_phone) {
echo __('Recipient Phone Number') . ': ' . $gift_phone . "n";
}
} else {
echo '<h2>' . __('Gift Delivery Details') . '</h2>';
echo '<ul>';
if ($gift_eircode) {
echo '<li><strong>' . __('Recipient Eircode') . ':</strong> ' . esc_html($gift_eircode) . '</li>';
}
if ($gift_phone) {
echo '<li><strong>' . __('Recipient Phone Number') . ':</strong> ' . esc_html($gift_phone) . '</li>';
}
echo '</ul>';
}
}
}
—
### ✅ **2. Optional: Add to the email meta fields (usually shown in a small table)**
If you want the fields added to the **default meta info table** (used in some email templates), you can keep your previous filter, but make sure it's accessing meta properly:
add_filter('woocommerce_email_order_meta_fields', 'add_gift_delivery_fields_to_meta_table');
function add_gift_delivery_fields_to_meta_table($fields) {
$fields['gift_eircode'] = array(
'label' => __('Recipient Eircode'),
'value' => function($order) {
return $order->get_meta('gift_eircode');
},
);
$fields['gift_phone'] = array(
'label' => __('Recipient Phone Number'),
'value' => function($order) {
return $order->get_meta('gift_phone');
},
);
return $fields;
}
—
### 💡 Summary
– Use `woocommerce_email_order_details` for custom content block.
– Use `woocommerce_email_order_meta_fields` for small order details table.
– Always prefer `$order->get_meta()` for consistency with the `wc_orders_meta` table.
Let me know if you'd also like to:
– Style this block with custom CSS in email templates.
– Add conditional logic (e.g., only show if the order has a gift item).
– Make these fields appear in PDF invoices or packing slips.