skip to Main Content

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:domainscafe2wp-contentthemesenvo-shopperfunctions.php:501
Stack trace: #0 W:domainscafe2wp-settings.php(555): include() #1
W:domainscafe2wp-config.php(96):
require_once(‘W:domainscafe…’) #2
W:domainscafe2wp-load.php(50): require_once(‘W:domainscafe…’)
#3 W:domainscafe2wp-blog-header.php(13): require_once(‘W:domainscafe…’) #4
W:domainscafe2index.php(17): require(‘W:domainscafe…’) #5
{main} thrown in
W:domainscafe2wp-contentthemesenvo-shopperfunctions.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'];

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search