In this tutorial, we will discuss how to get a WooCommerce order ID. What is an order ID? WordPress treats WooCommerce products as custom post types and assigns them unique numeral identifiers- IDs. Consequently, each order has a unique ID as well, so WordPress could distinguish them when selecting information from the database. Let us take a look at common methods on how the WooCommerce order ID can be found.
WooCommerce Order ID from Database
If you are somewhat familiar with the inner workings of databases, then you can quickly look up the order ID there. Login to your phpMyAdmin account and find the wp_posts table. You will see a dedicated ID column. Go over the database and find the order ID you need.
Keep in mind that your database might have a different prefix than wp_, so make sure that you change it accordingly.
WooCommerce Order ID with Plugins
You might not be familiar with databases so consider using plugins. There are several noteworthy plugins on the market at the moment. We will list a few of the popular ones that you can use in order to get the order ID displayed on your dashboard.
- Reveal IDs – With the release of WordPress 2.5, all IDs for admin pages were removed due to the fact that the average user has no need for them. This open-source software brings back the functionality and you will be able to see IDs right next to WooCommerce orders.
- Sequential Order Numbers Pro – This plugin allows you to number orders in any way you want. For instance, you can add a date and time to order IDs. A highly useful tool for managing WooCommerce orders.
- Custom Order Numbers for WooCommerce – You can reset existing order IDs and create new ones according to your specifications. The most distinguishing feature is the ability to re-name existing order IDs.
WooCommerce Order ID via PHP
For customization purposes, there may be a need to display the WooCommerce product ID somewhere in the front-end, for instance, an e-mail to the customer to confirm a successful order. In order to achieve this, the order ID must first be included in the code so WordPress can use it in the functions. Take a look at the code below.
//Declare the global WooCoomerce Object & Post Object
global $woocommerce, $post;
//Assign the order ID using the $post->ID
$order = new WC_Order($post->ID);
// Use the getter function to get order ID
As you can see, you can use the last function to call upon the specific order ID.
It is up to you to decide which one of these methods best suits your purpose. In web development, there is no such thing as only one correct answer. We wish you a bug-free journey through the world of eCommerce and stay tuned for more articles!