A product ID in WooCommerce is a number that’s used by WordPress and WooCommerce identify a product in your store. When you create a product in WooCommerce, it generates a corresponding ID for your product.

The product ID is identified by the variable product_id or $product_id internally within your WordPress site.

In this post, we’ll outline how you can find the WooCommerce product ID of a product within your WooCommerce store.

While you may never encounter the product ID when creating and displaying a product through its permalink (a permalink or permanent link is a URL that is intended to remain unchanged for many years into the future), there are a few reasons you might want to know what your product ID is:

  • You need the product ID to use in a shortcode that highlights a product within another page.
  • To create a list in WordPress, or you need the product ID.
  • You might need a product ID to create advanced product filters.

So now we’ve looked at why you might need to use a product ID, let’s explore how you can find the WooCommerce product ID in your store.

Get the WooCommerce product ID in the back-end of WordPress

There are two simple ways of finding out the product ID from the back-end of WordPress. When you edit a product, the URL has a bunch of extra information within it that reveals the product ID.

For instance, a typical URL that’s displayed when editing a product might look something like this:

www.example.com/wp-admin/post.php?post=22&action=edit
Get Product ID in WooCommerce from URL
Get Product ID in WooCommerce from URL

The keyword post contains the product ID of the product that you are editing. In the example above, the Product ID is 22.

A second option is to head over the Products page in your WordPress Admin. In this listing, you’ll find the WooCommerce product ID when you hover over a product name. You can additionally search for your product using the product SKU name or product name and hover over the search results to get the Product ID.

Get Product ID in WooCommerce from Product Page
Get Product ID in WooCommerce from Product Page

Get the product ID using SQL and PHP

While the methods described above are easy to perform, they are generally more time consuming if you have a large number of products in the pipeline.

If you’re technically inclined and understand PHP, there’s a more efficient way to find the WooCommerce product ID programmatically. There are two ways of doing this: through SQL and PHP.

A WooCommerce product is essentially a WordPress post, therefore, any product that you create in WooCommerce is stored in the corresponding posts database table. The SKU name and product ID are stored in the posts metadata table in WordPress.

In the following query to get the WooCommerce product ID, we assume that your WordPress database table prefix is “wp_”.

SELECT post_id FROM wp_postmeta WHERE meta_key='_sku' AND meta_value='sku-name';

If you have a list of SKU names, you can simply get the WooCommerce product ID of each product in that list using this SQL query.

Additionally, you can use PHP to get the Product ID of a specific product. If you are at a product page, the following snippet of code saves the product ID in your $id variable, which you can then use to display the ID on the page.

global $product;
$id = $product->get_id();

While this enables you to get the product ID of a single product at a time, you can use a function in PHP to recursively query the table to get product IDs of multiple products. Use the following snippet to accomplish the 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)
  );
}

Additionally, you can wrap this query around a function and put it in your functions.php file to reuse later.

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;

}

You can then use this function in any part of your WordPress site.

$product_id = iconic_get_product_id_by_sku('your-sku');

Now that you know how to get the product ID in a few different ways, we’ll explore how to use it in a few Iconic plugins.

Use the product ID with Show Single Variations

WooCommerce Show Single Variations allows you to display multiple variations of the same product in your shop as though they are separate products. This allows you to efficiently manage the variations of the same product under a single product.

Product Variations in WooCommerce
Product Variations in WooCommerce

If a customer goes to the checkout page with separate variations of the same product, these cart items would be treated as separate products. During the checkout of the WooCommerce order, you can set the cart items to also be priced differently using this plugin too.

While Show Single Variations allows you to create variations of a single product, they are created as different products in the background. These variations are displayed on a single product page. To get the product ID of a variation, check the number next to it in the Variations tab. This could be useful in managing variations. You can set the product price for each variation too.

Use the product ID for advanced product filtering

You can further use these product variations to create advanced filters. When creating these filters, you can exclude specific products or product variations using their product ID. Products may also be selectively added or removed from categories. Creating a new category creates a category ID, which can thus be associated with the product ID.

Roundup: Using your WooCommerce Product ID

While you may never encounter the product ID of a WooCommerce product through the normal product creation process, it may be required in various cases like adding shortcodes or creating filters.

In this article, we discussed multiple ways of finding the WooCommerce product ID including:

  • Getting the Product ID from the URL when editing a product in WordPress back-end.
  • By hovering over a product in the product list page in your WordPress back-end.
  • If you have access to the database that WordPress runs on, you can query for the product ID directly using the product SKU name.
  • You may use the existing WooCommerce class in PHP to access and display the product ID.
  • You can build a custom function to store in the functions.php file of WordPress, which uses the SQL queries from before.

Then, we looked at various use cases of the product ID while creating variations of the same product using WooCommerce Show Single Variations.

WooCommerce Show Single Variations

Display individual product variations of a variable product in your product listings. Make it easy for your customers to view and filter product variations.