wordpresswoocommercewoocommerce-bookings

Access WooCommerce Order Details in Custom Plugin


i'm guessing this is a simple thing that I can't seem to wrap my head around. I'm trying to write a little custom plugin that requires grabbing the data of a order made in WooCommerce & WooCommerce Bookings. The issue i'm having though is that nothing seems to be returning when I run the function:


function middleground_function(){

    $order = wc_get_order(37340);

    print_r($order);
}



add_action( 'woocommerce_after_register_post_type ', 'middleground_function' );

I don't get any errors but I don't get any print. So i'm wondering what i'm missing here!

When it goes to production that action will change to woocommerce_payment_complete but I figured i'd be able to atleast test it with a test ID

Thank you for your help!


Solution

  • Not sure why you are using the woocommerce_after_register_post_type action hook. but if you want just to test whether you can get order details by id or not you can use the init hook. try the below code.

    function middleground_function(){
        $order = wc_get_order( 37340 );
        echo "<pre>"; print_r( $order ); echo "</pre>";
    }
    add_action( 'init ', 'middleground_function' );