How to Get WooCommerce Current Order ID from URL

WooCommerce Get Current Order ID from URL

In this post, I show you how to get the current order ID from the URL in WooCommerce and also share a code snippet you can use to get the order ID.

In the previous post on WooCommerce get order ID.

I highlighted how to get an order ID from the database and from the WooCommerce admin settings page.

You can get the current order ID on the WooCommerce checkout page or the order page from the URL, as I will show you in the example below.

Why Get WooCommerce Current Order ID from URL?

There are several scenarios you may want to implement with the order ID from the URL.

The most common is to use the current order ID from the URL to control the checkout process and redirect customers after checkout based on the order.

Before we look at how to get the current order ID from the URL, you need to first understand how WooCommerce orders work.

WooCommerce Get Current Order ID from URL

If you are on the checkout page and you place the order, there are a number of parameters that are placed in the URL, and you can see them as shown on the image below:WooCommerce Get Current Order ID from URL

As you can see, one of the parameters added to the WooCommerce URL during the checkout process is the order ID. The order ID is also added to the WooCommerce thank you page, as shown on the image below :WooCommerce Get Current Order ID from URL

So let us now find out how we can grab this order ID from the URL and use it in any other logic in WooCommerce development.

How to Get WooCommerce Current Order ID from URL

To get the WooCommerce current Order ID from the URL we need to first determine if we are on the order received page.

We can do this using the WooCommerce function to check the endpoint as in the code below:

is_wc_endpoint_url( ‘order-received’ )

Now that we have determined we are on the order received page we are now sure we can get the parameters I talked about before and use them in our code.

In this case, we will easily grab the ID I showed you in the image above. So in this case we need to use one of the WordPress global objects to get the full URL of the current page – global $wp.

$wp (object) The global instance of the WP class.

We can now strip the URL parameters to extract the order ID we want from the URL. The following is the full code snippet to get WooCommerce order from the URL:

 

if ( is_wc_endpoint_url( 'order-received' ) ) {

global $wp;

//Get Order ID

$order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

}

 

Get the Order ID from the URL and Display it in the Header 

I want to add this code to a header action hook for the demonstration to showcase how you can easily get the order ID from the URL using the code snippet I have shared above.

The following is the full code snippet with the action hook to the header for illustration:

 

// WooCommerce Get Current Order ID from URL

add_action('wp_head', 'get_current_order_id_from_url');

function get_current_order_id_from_url(){

if ( is_wc_endpoint_url( 'order-received' ) ) {

global $wp;

//Get Order ID

$current_order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

echo $current_order_id;

}

}


After you add this code snippet to your test theme, like my Customized Storefront theme, you should see the order ID displayed when you complete the checkout as shown on the image below:WooCommerce Get Current Order ID from URL

Conclusion

In this post, I have demonstrated how you can get the WooCommerce order ID from the URL. There are basically two steps you need to understand.

First, you need to find out if you are on the order received page that is shown after WooCommerce redirects after checkout.  The second step is to extract the ID from the URL with the help of the WordPress global $wp object.

Similar Articles