Custom Column WooCommerce Orders
January 30, 2025 Β· AI Conversations
// Add custom column to orders list
add_filter('manage_edit-shop_order_columns', function ($columns) {
print_r($columns);
$columns['order_source'] = __('Order Source', 'your-textdomain');
return $columns;
});
not working
### **1. Ensure Hook is Applied Properly**
The correct hook for adding custom columns to WooCommerce orders is:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$columns['order_source'] = __('Order Source', 'your-textdomain');
return $columns;
}, 20);
– Ensure WooCommerce is active.
– Ensure the function is added in `functions.php` or a custom plugin.
### **2. Add Data to the Custom Column**
Adding the column alone won't display data. You need to fill it with content using the `manage_shop_order_posts_custom_column` hook:
add_action('manage_shop_order_posts_custom_column', function ($column, $post_id) {
if ($column === 'order_source') {
// Example: Fetching from order meta
$order = wc_get_order($post_id);
echo esc_html($order->get_meta('_order_source', true) ?: 'N/A');
}
}, 10, 2);
– Replace `_order_source` with the actual meta key where your order source is stored.
### **3. Ensure Column Order**
By default, WooCommerce reorders the columns. If your custom column doesn't show, try adjusting its position:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$new_columns = [];
foreach ($columns as $key => $title) {
$new_columns[$key] = $title;
if ($key === 'order_total') { // Insert after Order Total
$new_columns['order_source'] = __('Order Source', 'your-textdomain');
}
}
return $new_columns;
}, 20);
– This places the "Order Source" column after the "Order Total" column.
### **4. Check for Conflicts**
If it's still not working:
– Check the browser console for JavaScript errors.
– Temporarily disable other plugins to see if there's a conflict.
– Switch to a default theme (like Twenty Twenty-Four) to test.
Let me know if you need further debugging! π
### **Steps to Add a Custom Column to WooCommerce Orders List**
#### **1. Add a New Column**
Modify the `manage_edit-shop_order_columns` filter to add your custom column:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$columns['order_source'] = __('Order Source', 'your-textdomain'); // Change 'Order Source' to your desired column name
return $columns;
});
#### **2. Populate the Column with Data**
You need to use the `manage_shop_order_posts_custom_column` action to display data in the column:
add_action('manage_shop_order_posts_custom_column', function ($column, $post_id) {
if ($column === 'order_source') {
$order = wc_get_order($post_id);
$order_source = $order->get_meta('_order_source', true); // Replace '_order_source' with your actual meta key
echo !empty($order_source) ? esc_html($order_source) : __('N/A', 'your-textdomain'); // Display 'N/A' if no data found
}
}, 10, 2);
#### **3. Ensure the Column Appears in the Correct Position**
WooCommerce may override column orders. To ensure the new column appears at the right position, modify it like this:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$new_columns = [];
foreach ($columns as $key => $title) {
$new_columns[$key] = $title;
// Add our custom column after 'order_total'
if ($key === 'order_total') {
$new_columns['order_source'] = __('Order Source', 'your-textdomain');
}
}
return $new_columns;
}, 20);
#### **4. Save Order Source in Order Meta (If Not Already Stored)**
If you haven't stored the order source in order meta, you need to save it when the order is created:
add_action('woocommerce_checkout_update_order_meta', function ($order_id) {
if (!empty($_POST['order_source'])) {
update_post_meta($order_id, '_order_source', sanitize_text_field($_POST['order_source']));
}
});
You can set `_order_source` dynamically based on payment method, user role, or any other condition.
—
### **Testing**
1. Add the above code to your theme's `functions.php` file or a custom plugin.
2. Ensure `_order_source` meta exists in your database (you can check in `wp_postmeta`).
3. Reload the **WooCommerce β Orders** page in wp-admin and confirm the column appears.
Let me know if you need further adjustments! π
—
## **Debugging & Fixing the Issue**
### **1. Verify Hook Execution**
Before making further changes, letβs check whether the `manage_edit-shop_order_columns` filter is being executed.
**Add this temporary debug statement to check column output:**
add_filter('manage_edit-shop_order_columns', function ($columns) {
error_log(print_r($columns, true)); // Log current columns
$columns['order_source'] = __('Order Source', 'your-textdomain');
return $columns;
});
– Open your `debug.log` file (`wp-content/debug.log`) and check if the column array appears.
– If nothing appears in the log, your function is not executing.
—
### **2. Ensure WooCommerce Admin is Loaded**
WooCommerce might not be fully loaded when the hook runs. Try adding a priority:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$columns['order_source'] = __('Order Source', 'your-textdomain');
return $columns;
}, 20); // Higher priority ensures WooCommerce is loaded
—
### **3. Ensure Column Data Appears**
If the column appears but has no data, ensure it gets populated:
add_action('manage_shop_order_posts_custom_column', function ($column, $post_id) {
if ($column === 'order_source') {
$order = wc_get_order($post_id);
$order_source = $order ? $order->get_meta('_order_source', true) : ''; // Get custom order meta
echo !empty($order_source) ? esc_html($order_source) : '<span style="color:red;">N/A</span>';
}
}, 10, 2);
**Test:**
– Edit an order in WooCommerce.
– Manually add a custom field `_order_source` in the "Custom Fields" section.
– Refresh the orders list to see if the value appears.
—
### **4. Check for WooCommerce Overriding Columns**
WooCommerce might override columns. Modify this:
add_filter('manage_edit-shop_order_columns', function ($columns) {
$new_columns = [];
foreach ($columns as $key => $title) {
$new_columns[$key] = $title;
if ($key === 'order_total') { // Insert after "Total"
$new_columns['order_source'] = __('Order Source', 'your-textdomain');
}
}
return $new_columns;
}, 99);
—
### **5. Ensure WooCommerce is Active**
Run this check:
if (!function_exists('wc_get_order')) {
error_log('WooCommerce is not active!');
}
If you see "WooCommerce is not active!" in `debug.log`, your WooCommerce plugin might not be properly loaded.
—
### **6. Try Inside a Plugin Instead of functions.php**
If the theme is interfering, move the code into a plugin:
1. Create a file `custom-order-column.php` inside `wp-content/plugins/`
2. Paste this:
<?php
/**
* Plugin Name: Custom Order Column
*/
add_filter('manage_edit-shop_order_columns', function ($columns) {
$columns['order_source'] = __('Order Source', 'your-textdomain');
return $columns;
}, 99);
add_action('manage_shop_order_posts_custom_column', function ($column, $post_id) {
if ($column === 'order_source') {
$order = wc_get_order($post_id);
$order_source = $order->get_meta('_order_source', true);
echo !empty($order_source) ? esc_html($order_source) : '<span style="color:red;">N/A</span>';
}
}, 10, 2);
3. Activate the plugin via **wp-admin β Plugins**.
—
## **Final Steps**
1. **Clear any cache** (WP Super Cache, W3 Total Cache, Cloudflare).
2. **Disable other admin column-related plugins** that may interfere.
3. **Try switching to a default theme** (like Twenty Twenty-Four) and test again.
—
Let me know if it still doesn't work, and we can debug further! π