How to Get the Product ID in WooCommerce
The importance of WooCommerce product ID has become obvious. It has been an integral part of your WooStore. And it also contributed significantly to the customization of your shop page. Therefore, every store owner needs it to manage his/her online store better.
By knowing how to get the product ID in WooCommerce, you could have a deeper understanding of your product details, along with how users will interact with your website. Do not hesitate to accompany us to explore effortless ways to get your product ID. It is super simple to do, so let’s thrive!
Why do you need to get a WooCommerce product ID?
As a unique and essential number assigned to each product in your WooStore, product IDs are useful in identifying your store’s products in myriad scenarios. There are some cases that you need to get your product ID.
- Redirect after product checkout
- Use in the shortcode
- Create advanced filters for products
Product IDs are helpful in creating advanced filters for your products. Hence, it is a need to get your WooCommerce product ID if you need to improve your store as well as create a WordPress list.
This is to say, it plays an indispensable part in customizing your shop page within WooCommerce, not to mention other functionalities such as cart or checkout.
How to get the product ID in WooCommerce?
To get your WooCommerce product ID, there are several easy ways to follow. So, keep scrolling down to explore them and equip yourself with the most convenient one.
1. Get the WooCommerce product ID in WordPress’s backend
To get the WooCommerce product ID in WordPress’s backend, there exist two easy ways. When you proceed to edit any product, the product URL comes with a number of additional information. Here, the information will reveal your product ID.
Let’s take an example. The URL that is presented while you edit a product may look like the below screen:
As you could notice, “post” is the keyword to contain your product IDs of specific WooCommerce products in which you are about to edit. In the above screen, you can see that “22” is a product ID.
The second way you can consider getting your WooCommerce product ID is to move in the direction of your page Products in the area of WordPress Admin, when you are here, you could see the list that contains product ID when hovering over one certain product name. Additionally, searching for a product with the use of an SKU name as well as product name or hovering over your search results are also simple and useful ways to get your product ID.
2. Get the WooCommerce product ID with the use of PHP & SQL
In the first method, we mention using WordPress’s backend. While the first one is simple to implement, it is time-consuming in case you want to get IDs for tons of awaiting products. We also recommend a second method which involves using PHP, coupled with SQL to get the WooCommerce product ID. Hence, if you are good at PHP and technically inclined, this way is a very effective one to do.
Because a product in WooCommerce is necessarily one WordPress post, every product created in this platform is kept in the respective posts in the database table. On the other hand, the product ID and SKU name get stored in WordPress’s posts in the metadata table.
Assume that “wp_” is the prefix of your WordPress’s database table, you can get the product ID in the below query:
SELECT post_id FROM wp_postmeta WHERE meta_key='_sku' AND meta_value='sku-name';
If you have SKU names, what you could do to get product IDs is using the SQL query. Furthermore, using PHP to find your product IDs for specific products is also an excellent way to go. When you come to your product page, you could utilize the below code snippet that helps to save your product ID within WooCommerce in the “$id” variable to show the ID on your page.
global $product;
$id = $product->get_id();
Do you know that you could even get multiple products’ IDs? The above code facilitates you in getting one single product’s ID at one time. It is to use one of the functions in PHP for querying the table recursively. To do that, apply the below snippet to execute your task:
$sku_names = array('sku_name1', 'sku_name2', 'sku_name3');
global $wpdb;
foreach($sku_names as $sku_name){
$product_id = $wpdb->get_var(
$wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s'", $sku_name)
);
}
What is more, this query can be wrapped around one function, then is put into the file “function.php” for later reuse:
function iconic_get_product_id_by_sku($sku = false){
global $wpdb;
if(!$sku)
return null;
$product_id = $wpdb->get_var(
$wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1",
$sku)
);
if ($product_id)
return $product_id;
return null;
}
The great thing is that you could apply this kind of function to different parts in your WordPress website.
$product_id = iconic_get_product_id_by_sku('your-sku');
3. Get the individual WooCommerce product ID from the WordPress dashboard
The third simple way to get your product ID is to find it within your WordPress dashboard. You can find it on your product page. Navigate to the area of WordPress Admin, choose “Products” and click “All Products”. Then you will hover over products that you want to know the ID. This ID will appear under the name of any product.
4. Get all WooCommerce product IDs from the WordPress dashboard
Besides getting an individual product ID for a single WooCommerce product, you could also get all IDs of all products which are listed in your CSV file.
The first thing to do within WooCommerce is to approach the menu “Products”. After that, you can see the button “Export”, thus click it. The next step involves choosing the button “Generate CSV”. When this CSV file is exported, you can open it to check each product ID of your WooCommerce products in the 1st column named “ID”.
Conclusion
There is no denying that you will need your product ID in various situations, including constructing product filters or putting in the shortcode for any customization. So, hope you could enjoy this quick guide to the utmost. Read it and save time getting your product ID. Follow us for more useful tips and tricks in the future.