I have added a simple custom field to my WooCommerce orders where customers must provide a pickup time when they order something. This field gets saved in post_meta table as a simple date string.
Now I wanted to show that field in the orders index in Wordpress Backend and also in order detail. In detail it is showing the correct value but in index it just shows a duplication of the newest set date.
Here's my code:
add_filter( 'manage_edit-shop_order_columns', 'register_pickup_time_column', 10, 1 );
function register_pickup_time_column( $columns ) {
$columns['pickup_time'] = 'Pickup time';
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column', 'display_pickup_time_column', 10, 1 );
function display_pickup_time_column( $column ) {
global $post;
if ( 'pickup_time' === $column ) {
$pickup_time = get_post_meta( $post->ID, 'Pickup time', true );
echo $pickup_time;
}
}
I cross checked the value in database with one of the order which shows an incorrect pickup time. The "Pickup time" meta field is definetly correct for each order.
How is that possible?
EDIT: I have found that it seems to loose the context of the post/order ID on the custom column. Check comments:
function display_pickup_time_column( $column ) {
global $post, $woocommerce, $the_order;
$order_id = $the_order->id;
// Correct ID is shown but only in default WooCommerce-Columns
var_dump($order_id);
if ($column === 'pickup_time') {
// Incorrect order ID is shown (duplicate from latest order)!
var_dump($order_id);
}
}