Do you want to get the order ID in WooCommerce? In today’s post, we will illustrate how you can achieve this.
There are several ways to get order ID You can use each of these methods to get the product order ID and use it in your code or plugin.
To understand how the code that retrieves the order ID works, you need to have a general idea of how the IDs work in WooCommerce and more so in WordPress.
Ideally, each page or post published in WordPress is assigned a special number that is unique to identify that number or post and this number is what is called a page ID or post ID.
In the past, I have written a post on how to find page IDs in WordPress, and there are also excellent plugins that can help you know the page ID from your WordPress dashboard.
Let us get right into it.
What is an Order ID
In WooCommerce the product is just a custom post type like any other custom post type in WordPress. These products and orders are assigned page IDs like any other WordPress post type.
The order ID is the unique number that is assigned to the order once it is created for identification and reuse in various other WooCommerce functions.
The order ID can be obtained from the WordPress database and you can use several ways to get order id including; using a database directly, using a plugin, and using the code to get order ID dynamically.
Let us outline each of these 3 ways to get a WooCommerce order ID.
Get Order ID from Database
It is important to note that you can get the order ID from the database.
WooCommerce Orders are placed in the same database table as the other WordPress posts. This table is the wp_posts, you can get order ID from this table from the ID column as shown below:
Check for the order and identify the corresponding order id from the ID column in the wp_post database table.
As I mentioned in the introduction the products and orders are custom post types and you can find their IDs in the database as shown on the image below for the product ‘Braker All-Weather’
Get Order ID Using Plugin
To get the product or order IDs quickly from your WordPress dashboard you can use a plugin like Reveal IDs WordPress plugin.
When you install this plugin you can see the IDs for WooCommerce products or Order ID from the dashboard as shown on the image below:
How to Get Order ID
If you are creating a WooCommerce plugin or theme and want to customize the order page or want to send unique emails to the customer or shop owner after an order has been placed, you will be required to get order id in the code.
The following are code snippets that will help you get the order ID and use it in your functions:
<?php //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 $order->get_id();
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Conclusion
In this post, we have outlined the 3 possible ways you can use to get order ID in WooCommerce. Each of these methods has its unique advantage but they are all useful when you are creating WooCommerce plugins or themes and want to quickly or easily get the order ID.
The last method requires some basic skills to implement. Therefore, you need to create a backup of your website. This allows you to revert to the previous version if you make an error.
We hope that this post helped you to get the order ID.
Comments are closed.