How to get the order ID in the order-received and view-order page?

For my Fancy Product Designer plugin I need to get the order ID in the order-received page and view-order page. Woocommerce offers a filter hook to get the ordered item link:  woocommerce_order_item_name, which is currently not documented in the filter hooks reference. I found it when I browsed through the woocommerce source code. Its located in the order-details.php template. For older version than 2.1 it was woocommerce_order_table_product_title.

Via this filter hook you can change the item link and receive an array that represents the item. The array contains following values:

Let’s see how it looks like when using the filter hook:

For my Fancy Product Designer plugin I need to get the current order ID, in older version it was very easy, because it was appended as GET parameter in the URL, so I could receive it via a simple $_GET request. It’s still available as GET parameter, when default permalinks are used. But for custom permalinks, it’s now shown as path in the URL, e.g. my-account/view-order/398/. I was browsing through the source code of woocommerce and was not thinking that it will be such a tough task to get the order ID, actually I thought woocommerce offers a simple solution to get the order ID of an item, but I could not find any simple solution, so I created this little dirty snippet:

First of all…if anyone has a simpler solution, please let me know via the comments.

  • ben
    Posted at 11:58h, 04 June Reply

    hey, i was facing the same problem and also searched for a better solution. in the end, i did it in a similar way as you did above.
    really strange, that there is no such function to retrieve the order id…

    • admin
      Posted at 07:48h, 05 June Reply

      Yes, but another problem is also that you can set the endpoint in Woocommerce 2.1. E.g. the slug for “order-received” could be also different, if the user decide to change it in the settings.

  • Gareth
    Posted at 15:30h, 26 December Reply

    I’m not sure if this is an outdated question. But with the latest version of Woocommerce a wc_order value can be retrieved using $_GET.

    You can do a quick query of the value in the wp_postmeta to get the post_id (which is the order ID).

    • admin
      Posted at 18:10h, 04 March Reply

      Yes, when using the default permalinks. But SEO-friendly permalinks are looking like that:

  • CK MacLeod
    Posted at 05:37h, 18 February Reply

    Your method DOES work, and that’s impressive. I wonder if you’ve in the months since you wrote this post run across a simpler way to get the order ID on the orders received page: My initial assumption was that if Woo is getting it for that page – via order-details.php, we ought to be able to use the same or similar methods.

    • admin
      Posted at 18:11h, 04 March Reply

      No, I am still using this method.

      • tooslider
        Posted at 18:06h, 04 May Reply

        just looked for an hour on this exact topic….wound up back here……still seems to be the way for me too

  • Andrey
    Posted at 11:33h, 08 June Reply

    global $wp;

    $order_id = $wp->query_vars[‘order-pay’];
    $order = new WC_Order( $order_id );

Post A Comment