1

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');
print_r($meeting_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.

  • Your print_r output won't be visible if $meeting_time is false or an empty string, you should surround it with something e.g. print_r( 'meetingtime: "'.$meeting_time.'"' ) – Tom J Nowell Dec 19 '16 at 0:59
  • Awesome, cheers Tom this makes perfect sense! I'm planning to run this when a new order is made at the 'woocommerce_new_order' hook which is during page load, is there anyway I can print the return of the variable somewhere? as the page goes to a new page after order and I can't seem to see it. – M.McI Dec 19 '16 at 1:11
  • error_log or a PHP debugger would do the trick, but you're not longer doing WP specific things at that point, you're doing general PHP things and would be better asking on Stack Overflow – Tom J Nowell Dec 19 '16 at 12:27
3

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.

| improve this answer | |
0

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:

print_r($order_time);
| improve this answer | |
  • Hmm ok awesome info, thanks for the response! I should of mentioned that It's a bookable product the user can select a time and I was trying to get the time they have chosen from the meta data – M.McI Dec 19 '16 at 0:52
  • I tried to test the output by writing it to a file but nothing seemed to be printed, is this just because I wasn't using the function correctly? $meeting_time = wc_get_order_item_meta($order_id, 'Time'); $dump_meeting_time = var_dump($meeting_time); $file = 'wp-content/debuggo.php'; file_put_contents($file, $dump_meeting_time); – M.McI Dec 19 '16 at 0:57
  • Writing data to a file is something new, I would avoid introducing new problems while testing existing ones, and I would always check the return values of functions ( file_put_contents is very unlikely to succeed there, not unless your PHP file is in the root folder, which it almost certainly isn't ) – Tom J Nowell Dec 19 '16 at 0:58
  • Ok cool thanks for letting me know! I'm running this at a hook 'woocommerce_new_order' so I think when I print_r the output it happens during page load so I don't get to see the print_r, is there an easy way around this? – M.McI Dec 19 '16 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.