I need help to display custom order metadata in WooCommerce orders list. I have added a new custom column called "Meeting Points" and "Meeting Date&Time."
Here is my code so far:
add_filter('manage_edit-shop_order_columns', 'add_meeting_points_column');
function add_meeting_points_column($columns) {
$columns = array_merge(
array_slice($columns, 0, array_search('order_date', array_keys($columns)), true),
array('meeting_points' => __('Meeting Points', 'text-domain')),
array_slice($columns, array_search('order_date', array_keys($columns)), null, true)
);
return $columns;
}
add_action('manage_shop_order_posts_custom_column', 'display_meeting_points_column', 20, 2);
function display_Meeting_Points_column($column, $post_id) {
if ('Meeting_Points' === $column) {
$order = wc_get_order($post_id);
if ($order) {
$meeting_points = wc_get_order_meta_keys($order->get_keys(), 'MeetingPoints', true);
echo !empty($meeting_points) ? esc_html($meeting_points) : esc_html__('Not Available', 'text-domain');
}
}
}
My code doesn’t work as expected, it doesn’t display the data.
What I am doing wrong? Your assistance will be appreciated.
2
Answers
There are multiple mistakes in your 2nd function, for example column string value should be
'meeting_points'
instead of'Meeting_Points'
, to populate the column with your custom metadata.Try the following code replacement (making sure that
MeetingPoints
andMeetingDate&Time
are the correct meta keys used inwp_postmeta
database table for your orders):Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
In admin orders list:
In database under
wp_postmeta
table:Related:
It will help to get the right meta key values. You can add this code to functions.php and then visit an order page in the Dashboard to see a list of all the meta keys for each product in the order in the output in the footer.