Debugging with functions.php

I’m trying to check the output of a variable in my functions.php file that runs after a new woocommerce order. The code will get the order time from a new order and store it in a variable for me to use elsewhere the code being:

$meeting_time = wc_get_order_item_meta($order_id, 'Time');

I’ve tried a few ways to print the information:

  • print_r
  • javascript popup box
  • writing to error log

but I can’t seem to get any of these to work as my php knowledge isn’t very good.

I want to be able to see the output of the variable to make sure it’s returning what I want.

Is anyone able to assist me?

Please and thankyou.

Solutions Collecting From Web of "Debugging with functions.php"

You can use var_dump() instead of print_r() – you get the type and the value of the variable and it will also work when your variable holds FALSE or NULL.

print_r( false ); # doesn't output anything
var_dump( false ); # output: bool(false)

print_r( NULL ); # doesn't output anything
var_dump( NULL ); # output: NULL

If you have arrays or objects to inspect, you could use a plugin like Kint Debugger to format the output into a more readable format.

wc_get_order_item_meta function requires an Order Item’s ID, not the Order ID.

wc_get_order_item_meta($item_id, $key, $single );

Because all orders are stored as posts, you can get the post datetime from the order ID, and use that instead.

$order = get_post($orderID);
$order_time = $order->post_date;

then you can print that out using: