How to Get Product ID in WooCommerce

product ID in WooCommerce is a unique number that is used by WordPress and WooCommerce to identify a product in your store. When you create a product in WooCommerce, it generates a unique ID for that product.

If you are running online store through WooCommerce on WordPress Platform, you might need to get the product ID for various purposes, such as creating custom links, adding shortcodes, modifying the product data or maybe for any reason.

In this article, I will show you how to get the product ID in WooCommerce using four different methods: from the dashboard, from the URL, from the PHP code and from database, so by following these methods, you will be able to find the product ID of any product in your WooCommerce store easily and quickly.

4 Ways to Get Product ID in WooCommerce

There are four common ways to retrieve product ID in WooCommerce, so let me show you them:

1) Get Product ID in WordPress WooCommerce Dashboard

To get product ID from WordPress Dashboard, Navigate to WooCommerce Tab on the left sidebar menu of Dashboard and click on Products. There in the list of products, find the product for which you want to get the ID. Hover your mouse over the product, and you will see ID of that product like this:

Seeing Product ID in Products tab of WooCommerce
How to Get Product ID in WooCommerce

2) Get Product ID From URL

Another way to get the product ID in WooCommerce is by extracting it from the product’s URL. To do so, navigate products tab where all products are listed, click on the product you want to get ID of to view its details. This will take you to the product’s individual page.

There, look at the URL in your web browser’s address bar, you will see a link similar to this:

https://yourwebsite.com/wp-admin/post.php?post=1214&action=edit
Seeing Product ID in URL

A WooCommerce product is stored as a WordPress post, so you will see that the product ID is indicated by “post=” followed by the numerical value. In this example, the product ID is “1214”

Also Read: How to Delete all Products from WooCommerce Easily

3) Get Product ID in PHP Code

If you need to get the product ID programmatically, you can use PHP code to retrieve it. Here’s a sample PHP code snippet to get the product ID in WooCommerce:

<?php
// Get the current product ID
global $product;
$product_id = $product->get_id();

// Output the product ID
echo 'Product ID: ' . $product_id;
?>

You can use this code on a single product page where it will fetch and display the product ID, so you can use it according to your need.

However, if you want to fetch more product information programmatically, like product ID, title, Product Description, price, Product SKU and more, here is code:

<?php
// Get the current product
global $product;

if (is_a($product, 'WC_Product')) {
    // Get the product ID
    $product_id = $product->get_id();

    // Get the product title
    $product_title = $product->get_title();

    // Get the product price
    $product_price = $product->get_price();

    // Get the product regular price
    $product_regular_price = $product->get_regular_price();

    // Get the product sale price
    $product_sale_price = $product->get_sale_price();

    // Get the product description
    $product_description = $product->get_description();

    // Get the product SKU
    $product_sku = $product->get_sku();

    // Get the product stock status
    $product_stock_status = $product->get_stock_status();

    // Get the product categories
    $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'names'));

    // Output product information
    echo 'Product ID: ' . $product_id . '<br>';
    echo 'Product Title: ' . $product_title . '<br>';
    echo 'Product Price: ' . $product_price . '<br>';
    echo 'Regular Price: ' . $product_regular_price . '<br>';
    echo 'Sale Price: ' . $product_sale_price . '<br>';
    echo 'Product Description: ' . $product_description . '<br>';
    echo 'Product SKU: ' . $product_sku . '<br>';
    echo 'Stock Status: ' . $product_stock_status . '<br>';
    echo 'Product Categories: ' . implode(', ', $product_categories) . '<br>';
} else {
    echo 'This is not a valid product.';
}
?>

Make sure to backup your site before editing PHP code.

4) Get Product ID from the Database

If you are trying to find a product ID from your WordPress website’s database, you have two ways to do it. The first way is to check it manually by looking at the product details, another way is by running SQL query.

To use either method, you would first need to access your WordPress database. To do so, log in to your hosting account, there look for the “Databases” tab and click on it.

Login to your hosting account and locate databases
Go to databases and click on Enter phpMyAdmin button on your site to open database

Now open your website’s database using phpMyAdmin. In the selected database, there should be a table named wp_posts. This table stores information about all the posts on your WordPress site, including products( as I said earlier, WordPress store products as a post).

then go to wp_posts

Within the wp_posts table, you can either manually browse through the entries or use the Search or Filter function to see only WooCommerce products, to do it, just enter = “Products in filter field” and change number of rows to as many as possible, there look for your specific product, and you will see ID there.

Seeing product ID in wp_posts table

However, If you want to retrieve the product ID from the WooCommerce database using an SQL query which I can say is easy way to get product ID rather then manually searching it. You can use a SQL query like this and enter it:

SELECT ID FROM wp_posts WHERE post_type = 'product' AND post_status = 'publish' AND post_title = 'Your product name';
entering SQL query in database

Replace 'Your Product Name' with the name of the product you’re looking for. This query selects the ID of a product with a specific name from the “wp_posts” table, where “post_type” is “product” and “post_status” is “publish.”

So, there are four ways to find a product’s ID in WordPress WooCommerce. But you might be wondering, how can you get the product ID for products that come in different variations: variable product (like different colors or sizes)? And is there a way to get the product ID for all your products? so let me show you that too.

How to get Product ID For a Variable Product in WooCommerce

Variable products in WooCommerce have multiple variations, each with its own product ID. To find the product ID for a specific variation of a variable product, follow these steps

Go to the specific Variable Product, scroll down below to variations tab, there you will see ID of each variation like this:

checking ID of variable products in variable Tab

How to Get Product ID of All Products

If you need to obtain the product IDs for all your products, then go to Products, there click on export button, it will export it as CSV file which you can open in Excel, there you will see the list of all products IDs:

Exporting all products from WooCommerce
generate CSV file here
These are product IDs of all products

FAQ( How to Get Product ID in WooCommerce )

How can I find the Product ID in WooCommerce?

You can find the Product ID in WooCommerce by going to your WordPress admin, navigating to WooCommerce > Products, and there hover your mouse over the product you want to find the ID for. You will see the Product ID beside the edit option.

Can I change a Product ID in WooCommerce?

No, WooCommerce does not allow you to manually change a Product ID once it’s assigned. It’s a unique identifier generated by the WordPress system which should remain constant for each product.

Is the Product ID the same as the SKU (Stock Keeping Unit)?

No, the Product ID and SKU are different. The Product ID is a unique identifier for WooCommerce’s internal use, while the SKU is typically a user-defined code used for inventory and product management.

How do I get my product ID from a variation ID in WooCommerce?

To get the product ID from a variation ID in WooCommerce, use the following PHP code:

$variation_id = 123; // Replace with your variation ID $product_id = wp_get_post_parent_id($variation_id);

Now, $product_id holds the product ID associated with the variation.

Wrapping it Up

Knowing how to get product IDs in WooCommerce is essential for managing your online store effectively. Whether you need to retrieve product IDs from the WooCommerce dashboard, product edit page, or programmatically through PHP code or SQL query, the methods outlined in this guide will help you access product ID according to your need.

Whether you are a WooCommerce store owner or a developer, understanding and working with product IDs will help you to create a more customized and efficient e-commerce experience for your customers.

I hope this guide would have helped you find the product ID in WooCommerce. Still if you have any further quires, just comment down below and I will respond to you in no time. Thanks for reading…!

You May Like to Read:

How to Remove Add to Cart Button in WooCommerce (9 Ways)

How to Change Out of Stock Text in WooCommerce Easily

How to Change WooCommerce Button Color ( 4 Easy Ways )

Hamza Afridi is a Full stack Web & WordPress developer and writer with 5+ years of experience. He is Founder of webtalkhub.com, a blog on web development, SEO, and digital marketing tutorials. He enjoys learning and sharing new technologies.

Leave a Comment