0

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);
    }
}
Mango D
  • 501
  • 3
  • 11
  • 28
  • _"it just shows a duplication of the newest set date"_ - not really clear what that is supposed to mean. Can you please give an example? – CBroe Feb 25 '22 at 07:30
  • In the WooCommerce orders table it show the same pickup time for all orders. It shows the pickup time of the newest order. – Mango D Feb 25 '22 at 07:35
  • If you stored this meta value directly on the order, then I am not sure whether you should be using the global $post to read it. Searching for the name of the hook, I find https://wordpress.stackexchange.com/q/264328, where I see people explicitly accessing `global $the_order;` to get access to the order object. – CBroe Feb 25 '22 at 07:40
  • Hm I tried it the way with `global $the_order` but still it doesnt show the correct date. I did a `var_dump(get_post_meta($the_order->id))` now and I can see the correct meta date in the payload! I am absolutely confused how this is possible... – Mango D Feb 25 '22 at 08:26
  • I have added an EDIT to my question. – Mango D Feb 25 '22 at 08:35
  • `if ($column === 'pickup_delivery_date')` - is that an additional column now? The one you added was named `pickup_time`, no? – CBroe Feb 25 '22 at 08:38
  • @CBroe Sorry, corrected that. Just adapted the naming from my original code to simplify. – Mango D Feb 25 '22 at 08:41
  • And when you echo the order id in that second if block as well, is it still 30919? – CBroe Feb 25 '22 at 08:43
  • Ah no... It echoes the "copied" ID of the latest order. Very odd. It just looses its context outside of the default woocommerce columns. – Mango D Feb 25 '22 at 08:47
  • Every explanation / guide I can find on how to do this, appears to do pretty much the same thing. Can you, for testing purposes, add your column at the very start of the columns array (remove `$columns['pickup_time'] = 'Pickup time';`, and `return ['pickup_time' => 'Pickup time'] + $columns;` instead), and see if that changes anything? (Stab in the dark here, but I currently see no _logical_ explanation.) – CBroe Feb 25 '22 at 08:57
  • The post id is already passed to the `manage_shop_order_posts_custom_column` hook, as 2nd parameter, so it is not necessary to use global variables. See this [answer](https://stackoverflow.com/a/61155291/11987538) – 7uc1f3r Feb 25 '22 at 09:04
  • @7uc1f3r Hm, no I get an error when I add the second parameter to the corresponding hook function. – Mango D Feb 25 '22 at 10:08
  • Use `add_action( 'manage_shop_order_posts_custom_column', 'display_pickup_time_column', 10, 2 );` and `function display_pickup_time_column( $column, $post_id ) {`. I believe you should get started with: [How to debug in WooCommerce 3+](https://stackoverflow.com/q/61740111/11987538) since we can't see which error messages you get (if you don't mention them) and the code/question you ask/use has already been [answered dozens of times](https://stackoverflow.com/search?tab=votes&q=%5bwoocommerce%5d%20manage_shop_order_posts_custom_column), so there are certainly enough references to be found. – 7uc1f3r Feb 25 '22 at 10:12
  • @CBroe What the heck. That works! I did exactly what you suggested and added the new column to the first position of the array. Now it shows the correct ID and therefore the correct date. Interesting thing is that the "Total" column now shows the duplicated ID but still has the correct value. – Mango D Feb 25 '22 at 10:15

0 Answers0