Home > OS >  Call to a member function get_data() on boolean
Call to a member function get_data() on boolean

Time:02-21

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'];

CodePudding user response:

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

CodePudding user response:

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.

  • Related