0

I am suffering from a bug. Can't get order information. At first, I thought that the problem was in some plugins, turned everything off. And then completely created a new WP with only Woocommerce and a standard theme. Still the same error. Please, help me.

Fatal error: Uncaught Error: Call to a member function get_data() on boolean in W:\domains\cafe2\wp-content\themes\envo-shopper\functions.php:501 Stack trace: #0 W:\domains\cafe2\wp-settings.php(555): include() #1 W:\domains\cafe2\wp-config.php(96): require_once('W:\domains\cafe...') #2 W:\domains\cafe2\wp-load.php(50): require_once('W:\domains\cafe...') #3 W:\domains\cafe2\wp-blog-header.php(13): require_once('W:\domains\cafe...') #4 W:\domains\cafe2\index.php(17): require('W:\domains\cafe...') #5 {main} thrown in W:\domains\cafe2\wp-content\themes\envo-shopper\functions.php on line 501 There has been a critical error on this website.

    $order_id = 12;
$order = wc_get_order( $order_id );

$order_data = $order->get_data(); // The Order data

$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
blosterr
  • 1
  • 2

2 Answers2

1

You should check if

$order = wc_get_order( $order_id );

returns a correct value.

I'd do it this way (untested):

$order_id = 12;
$order = wc_get_order( $order_id );
if ($order !== false) {
  $order_data = $order->get_data(); // The Order data

  $order_id = $order_data['id'];
  $order_parent_id = $order_data['parent_id'];
  $order_status = $order_data['status'];

  ....
}

Infos: https://wp-kama.com/plugin/woocommerce/function/wc_get_order

Marco
  • 3,470
  • 4
  • 23
  • 35
  • I did this. Thanks. The result is false. The order number is 100% correct. Where can I find the problem? – blosterr Feb 18 '22 at 23:39
  • Try to read all orders - just for double check: https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query – Marco Feb 18 '22 at 23:40
  • Answer is - Array ( [0] => 13 [1] => 4 [2] => 1 ) But I have one test order with id 12. I checked in database. It says #12, link says its 12 and ID plugin says it's 12. I tried to get info of orders in array - same error. So, my server has problems? – blosterr Feb 18 '22 at 23:48
0

So, the problem was that in wp you can't request orders before the custom post type is registered.

I used woocommerce_after_register_post_type hook like this:

add_action( 'woocommerce_after_register_post_type', 
'action_function_name_533' );
function action_function_name_533(){
$order = wc_get_order( $order_id );
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$name = $item->get_name();
//etc....
}
}

Hope this helps someone in the future.

blosterr
  • 1
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 20 '22 at 18:22