How to Access WooCommerce Order Details in Custom Plugin

actually i am creating a new plugin in this i want to get the order number and all other details but for now just the order number. For this i am using the following code which i get from Stackoverflow past answers.

 add_action( 'wp_head', 'get_order_num' );

function get_order_num($order_id) {


    global $woocommerce, $order;

$order = new WC_Order($order->ID);
print_r($order);
die();
//to escape # from order id
$order_id = trim(str_replace('#', '', $order->get_order_number()));
echo ($order_id);

}

But this is showing me result in 0

This is the result shows when i do print_r to it

 WC_Order Object
(
    [order_type] => simple
    [id] => 0
    [post] => 
    [order_date] => 
    [modified_date] => 
    [customer_message] => 
    [customer_note] => 
    [post_status] => 
    [prices_include_tax] => 
    [tax_display_cart] => excl
    [display_totals_ex_tax] => 1
    [display_cart_ex_tax] => 1
    [formatted_billing_address:protected] => 
    [formatted_shipping_address:protected] => 
)

I know that this is not showing me the order number in array. I want to know how to show it in

Best Answer

You can hook to woocommerce_thankyou_order_id. It’d work like this:

function my_beautiful_hook($order_id)
{
//Do some magic here
}

add_action('woocommerce_thankyou_order_id','my_beautiful_hook')

Leave a Comment