How do I retrieve the order ID in WooCommerce?
-
4Just to clarify: `order id` equals to `post id`, as found into Wordpress standard tables. `order number` is the code that represent WooCommerce's order as represented in WC tables. – Erenor Paz Sep 01 '16 at 09:09
6 Answers
Current method:
The current way of accomplishing this is by using this function:
$order->get_id();
That should return the order id without "#".
Old method:
In older versions of WooCommerce, you may need to access it as a property instead:
echo $order->id;

- 7,332
- 3
- 48
- 69

- 1,108
- 10
- 21
-
4This returns the ID of the order post, not the ID of the order. $order->get_order_number(); is the correct answer. – piersb Nov 05 '15 at 16:49
-
2When you search for something similar at Google this is the first post. `$order->id` isn't anymore supported, you will get error (Member has protected access). But if you want to get ID (not order number) you can do it with: `$order->get_id();` – emilushi Dec 19 '17 at 11:24
-
This is not working in the current version of Woocommerce, you should use $order->get_id(); like emilushi said. – MidouCloud Jan 30 '18 at 09:49
-
My answer was from 3 and a half years ago, for an answer that is, as of now, 4 years old. Hopefully woocommerce evolved and things had changed :). Just edited my answer to reflect the current solution. Thanks for pointing out the current way of doing this. – Elvis Fernandes Feb 27 '18 at 02:11
-
it worked. Just modified it
global $woocommerce, $post;
$order = new WC_Order($post->ID);
//to escape # from order id
$order_id = trim(str_replace('#', '', $order->get_order_number()));

- 191,379
- 34
- 261
- 317

- 646
- 6
- 8
-
3I just tried - `$order->get_order_number()` and it returned only the id ( There wasn't any # ). I didn't need to replace that #. Probably latest woocommerce update removed the #. Can anyone confirm? – Aajahid Jan 02 '16 at 11:50
-
3Yes, I can confirm that `$order->get_order_number()` only returns the id (there is no "#" character) since at least Woocommerce version 2.4 (up to and including 3.0 and higher). It can be overridden by plugins with a wordpress filter, `woocommerce_order_number` so it is best to assume it is for display only and might not be numeric. Thanks! – truemedia Apr 28 '17 at 05:20
-
is it possible to get ordered product instock information using $order object? – Yogesh Mar 18 '21 at 11:06
I didnt test it and dont know were you need it, but:
$order = new WC_Order(post->ID);
echo $order->get_order_number();
Let me know if it works. I belive order number echoes with the "#" but you can split that if only need only the number.

- 505
- 3
- 9
-
I did this before, it does't work, the problem is what must be in WC_Order for parameter, I think i must delete the cookie then reduce stock manually. – Kourosh Feb 07 '14 at 18:55
-
As of woocommerce 3.0
$order->id;
will not work, it will generate notice, use getter function:
$order->get_id();
The same applies for other woocommerce objects like procut.

- 91
- 1
- 4
$order = new WC_Order( $post_id );
If you
echo $order->id;
then you'll be returned the id of the post from which the order is made. As you've already got that, it's probably not what you want.
echo $order->get_order_number();
will return the id of the order (with a # in front of it). To get rid of the #,
echo trim( str_replace( '#', '', $order->get_order_number() ) );
as per the accepted answer.

- 609
- 9
- 22
As LoicTheAztec mentioned here: https://stackoverflow.com/a/67182262/4638682
You get get this in several ways, within several template. Like so:
$order_id = absint( get_query_var('view-order') );

- 574
- 8
- 17