WooCommerce Hook Example
August 7, 2025 · AI Conversations
—
### ✅ Use Case:
For example, if you want to display a custom order meta field value (e.g., `custom_order_note`) just below the customer details on the admin order screen.
—
### ✅ Sample Code:
Place this in your theme’s `functions.php` or a custom plugin:
add_action('woocommerce_admin_order_data_after_customer_details', 'add_custom_order_meta_to_admin_order', 10, 1);
function add_custom_order_meta_to_admin_order($order) {
$custom_value = get_post_meta($order->get_id(), '_custom_order_note', true);
if (!empty($custom_value)) {
echo '<div class="address">';
echo '<p><strong style="color: #2271b1;">Custom Note:</strong> ' . esc_html($custom_value) . '</p>';
echo '</div>';
}
}
—
### ✅ Notes:
– Replace `_custom_order_note` with the actual meta key you're using.
– You can style the output as needed using HTML/CSS.
– The hook only works on the admin order detail page (`wp-admin/post.php?post=ORDER_ID&action=edit`).
Let me know your exact meta key and expected output if you want help with your real use case.
—
### ✅ Step-by-Step Debugging
#### 1. **Make Sure You're Editing the Right Hook**
This hook only fires on the **WooCommerce admin order edit page**, not on emails or front-end.
**URL should look like:**
`/wp-admin/post.php?post=1234&action=edit`
Where `1234` is your order ID.
—
#### 2. **Test with Simple Output**
Try this exact test code to check if the hook is working:
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
echo '<div class="address"><p style="color:red;">This is a test output!</p></div>';
});
– Go to WooCommerce → Orders → Edit any order.
– You should see “This is a test output!” in red below the customer details.
**If this doesn't show**, then:
—
### 🛠️ Troubleshooting Checklist
| Check | Solution |
|——-|———-|
| ✅ Is WooCommerce active? | Confirm WooCommerce plugin is activated. |
| ✅ Is the action placed in `functions.php` of the active theme or in a plugin? | Place code at the bottom of `functions.php` of the active theme temporarily. |
| ✅ Is error reporting hiding issues? | Check browser console and `wp-content/debug.log` if WP_DEBUG is enabled. |
| ✅ Is the order status filtering out the hook? | Try with different order statuses (Processing, Completed). |
—
### ✅ If You Want to Display Meta Field Value
Once test output shows, try this:
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
$value = get_post_meta($order->get_id(), '_your_meta_key', true);
if ($value) {
echo '<div class="address"><p><strong style="color:#2271b1;">Custom Info:</strong> ' . esc_html($value) . '</p></div>';
}
});
Replace `_your_meta_key` with your actual meta key.
—
If nothing works even with the basic red text test, paste your exact code here and I’ll debug it line by line for you.
—
### ✅ 1. Add Debug Text First
Paste this **exact code** at the bottom of your active theme’s `functions.php` file:
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
echo '<div class="address"><p style="color:green;">✅ Hook is working! Order ID: ' . $order->get_id() . '</p></div>';
});
– Then go to: **WooCommerce → Orders → Edit any order**
– If this message doesn’t appear:
✅ Hook is working! Order ID: `xxxx`
…then the hook is **not executing**, or you're not on the right screen.
—
### ✅ 2. If That Works, Display Custom Meta
Now let’s display your custom meta. Use this template:
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
$custom_field = get_post_meta($order->get_id(), '_your_meta_key', true);
if (!empty($custom_field)) {
echo '<div class="address">';
echo '<p><strong style="color:#0073aa;">My Meta:</strong> ' . esc_html($custom_field) . '</p>';
echo '</div>';
} else {
echo '<div class="address"><p style="color:orange;">⚠️ No custom meta found for key: _your_meta_key</p></div>';
}
});
✅ Replace `_your_meta_key` with your actual key — e.g., `_delivery_instructions`, `_custom_order_type`, etc.
—
### 🔍 Still Not Showing? Try This:
1. ✅ Confirm the **meta key exists** for the order:
– Run this temporarily to log all meta keys:
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
$all_meta = get_post_meta($order->get_id());
echo '<pre style="color:#333;background:#f7f7f7;padding:10px;border:1px solid #ccc;">';
print_r($all_meta);
echo '</pre>';
});
This will show **all available keys** for the order. Use an existing one to test.
2. ✅ Make sure **no plugin or theme is removing the hook**. You can test with a default theme like **Storefront**.
—
Share your exact code + meta key if it still doesn’t show, and I’ll debug it line by line for you.
Since you're using:
> `woocommerce_admin_order_data_after_customer_details`
…and nothing shows (even plain HTML), we'll now:
—
## ✅ Test with Absolute Minimum Setup
**1. Copy-Paste This Into Your Active Theme’s `functions.php`:**
add_action('woocommerce_admin_order_data_after_customer_details', function($order) {
echo '<div class="address"><p style="color:red;">TEST OUTPUT - Hook works!</p></div>';
});
—
### ✅ What you MUST check now:
– ✅ Go to: `wp-admin → WooCommerce → Orders → Edit Order`
– ✅ You must see `TEST OUTPUT – Hook works!` right below Billing/Shipping.
—
### 🛑 If it STILL does not show:
There is **only one reason**: something is breaking or blocking the hook.
—
## ✅ Final Debug Checklist
| Check | Solution |
|——-|———-|
| 🧩 Are you in the correct admin page? | Must be `post.php?post=1234&action=edit` |
| 🧩 Does your theme override the order details template? | Rename your theme temporarily. Switch to **Storefront** theme and test. |
| 🧩 Is your custom code in the right file? | `functions.php` of the **active theme** or a **MU plugin** |
| 🧩 Is there a PHP error? | Enable WP Debug: in `wp-config.php` set:`define( 'WP_DEBUG', true );“define( 'WP_DEBUG_LOG', true );`Check `wp-content/debug.log` |
| 🧩 Are you using a plugin like `Admin Custom Order Fields`? | Temporarily deactivate plugins and re-check |
—
## ✅ Alternative Hook (Try This)
If for some reason this hook is being suppressed, try this alternative WooCommerce hook:
add_action('woocommerce_admin_order_data_after_order_details', function($order) {
echo '<div class="address"><p style="color:blue;">Alternate Hook Output</p></div>';
});
—
### 🔄 Last Resort: Add via JS
If absolutely blocked, you can inject it via JavaScript in admin:
add_action('admin_footer', function () {
$screen = get_current_screen();
if ($screen && $screen->id === 'shop_order') {
echo "<script>
jQuery(document).ready(function($){
$('.order_data_column_container').append('<div style="padding:20px;background:#eef;border:1px solid #ccc;margin-top:10px;">🔧 Injected Meta Info</div>');
});
</script>";
}
});
—
If none of this works, send me:
– ✅ A screenshot of your order edit screen (above billing area)
– ✅ Your theme name and whether any WooCommerce template override exists
I’ll give you a tailored fix within minutes.