I want to add my field as new column in all orders table in backend. I have tried the following but its not displaying. Anyone know about this? Please help.
// Add custom column to orders list table
add_filter('manage_edit-shop_order_columns', 'custom_order_columns');
function custom_order_columns($columns)
{
// Inserting the new column after 'order_status' column
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_status' === $column_name) {
$new_columns['contributor'] = __('Contributor', 'textdomain');
}
}
return $new_columns;
}
// Populate custom column with contributor information
add_action('manage_shop_order_posts_custom_column', 'custom_order_column_content', 10, 2);
function custom_order_column_content($column, $post_id)
{
if ($column === 'contributor') {
$contributor_id = get_post_meta($post_id, 'custom_order_contributor', true);
if ($contributor_id) {
$contributor = get_userdata($contributor_id);
if ($contributor) {
echo $contributor->display_name;
} else {
echo 'Not Assigned';
}
} else {
echo 'Not Assigned';
}
}
}
Use the following code version that also works with Legacy orders and High-Performance Order Storage (HPOS), displaying the "contributor" display name, if the contributor ID has been recorded as metadata on your WooCommerce orders.
Code goes in functions.php file of your active child theme (or active theme). It should work.