How to Get a WooCommerce Product ID (Admin and PHP)

The WooCommerce product ID is the product’s WordPress post ID. To see it, go to Products > All Products and hover over a product: WooCommerce shows “ID: 123” in the row of links under the name. In PHP, use $product->get_id() on a product page, or wc_get_product( $id ) to go the other way and load a product from its ID.

This guide covers both: the admin screens for store owners, and tested PHP for every place developers usually need the ID (templates, block themes, variations, SKUs, orders, the cart, and bulk queries).

Where you areCodeWhat you get
Single product template or product loop$product->get_id() or get_the_ID()Current product ID
Custom block on a product template$block->context['postId']Product ID from block context
You have an ID, need the productwc_get_product( $id )WC_Product object, or false
You have a SKUwc_get_product_id_by_sku( 'SKU' )ID (product or variation), or 0
You have a variation$variation->get_parent_id()Parent variable product ID
Order line item$item->get_product_id() / get_variation_id()Parent ID / variation ID (0 if none)
Cart item$cart_item['product_id'] / ['variation_id']Parent ID / variation ID (0 if none)
Every productwc_get_products( array( 'return' => 'ids', 'limit' => -1 ) )Array of IDs
Quick reference. Every row was run on WordPress 7.1.4 with WooCommerce 11.2.2.

What is a WooCommerce Product ID?

WooCommerce stores every product as a WordPress custom post type, which means each product gets a unique numeric ID assigned automatically (the same way every WordPress post or page does). The product ID is just the post ID for that product, so anything you know about getting a WordPress post ID also works for products.

You’ll need a product’s ID for plenty of common WooCommerce tasks: passing it to a shortcode or block that wants a specific product, hooking into a WooCommerce filter for one product, looking up related orders, or grabbing it inside a custom template.


How to Find a Product ID in WooCommerce

Go to Products > All Products in your WordPress dashboard.

Hover over a product title. A row of action links appears below it (Edit, Quick Edit, Trash, View), and WooCommerce puts the ID first in that row. For example, “ID: 26510” means 26510 is the product ID.

Product ID in WooCommerce

For a big catalog, use the search box at the top of the products list to find the product first, then hover over it.

You can also click the product’s Edit link. The URL of the edit screen contains the product ID as the post= query parameter, like /wp-admin/post.php?post=26510&action=edit.

Product ID in the admin dashboard URL

Finding a Product ID For a Variation Product in WooCommerce

A variable product has its own ID, and each of its variations gets a separate ID too. If you sell a T-shirt in Red, Blue and Green, that is four IDs: one for the parent T-shirt and one per color.

variation IDs in WooCommerce

To find a variation’s ID: go to Products > All Products, click Edit on the parent variable product, scroll to the Product data box, and open the Variations tab. Each variation row shows its ID as a #NNNNN badge in the header.

In code, a variation is a WC_Product_Variation object. get_id() returns the variation’s own ID and get_parent_id() returns the variable product it belongs to:

$variation = wc_get_product( $variation_id );

if ( $variation && $variation->is_type( 'variation' ) ) {
    $parent_id = $variation->get_parent_id(); // the variable product
}

// Going the other way: all variation IDs of a variable product
$variation_ids = wc_get_product( $parent_id )->get_children();

Get Current Product ID (using PHP)

Which function you want depends on where your code runs. I ran every snippet below on a fresh WordPress 7.1.4 install with WooCommerce 11.2.2 and PHP 8.3 (in WordPress Playground), once with legacy order storage and once with HPOS enabled. The results were identical.

In a Single-Product Template (the global $product object)

In a classic theme’s WooCommerce templates (single-product.php, content-single-product.php, or a function attached to one of the woocommerce_ action hooks on a product page), the global $product object is already set:

global $product;

if ( $product instanceof WC_Product ) {
    $product_id = $product->get_id();
}

WooCommerce fills that global on the the_post action, so it works inside any loop over products, including your own WP_Query. Because products are posts, get_the_ID() returns the same number inside the loop:

$product_id = get_the_ID();

In a Block Theme or a Custom Block

Block themes build the Single Product template out of blocks instead of PHP template files, so there is no template file to drop global $product into. If you are writing your own block that sits on that template or inside a Product Collection, read the product ID from block context, which is exactly what WooCommerce’s own Product Price and Product SKU blocks do in their render code:

// block.json
"usesContext": [ "postId" ]

// render.php
$product_id = isset( $block->context['postId'] ) ? (int) $block->context['postId'] : 0;
$product    = $product_id ? wc_get_product( $product_id ) : false;

if ( $product ) {
    echo esc_html( $product->get_name() );
}

Block context is more reliable than the global here, because inside a Product Collection block each card gets its own postId, while global $product depends on whether a loop has run.

Get a Product by ID: wc_get_product()

When you already have an ID (from a setting, a shortcode attribute, an order) and want the product’s data, load the product object with wc_get_product():

$product = wc_get_product( 26510 );

if ( $product ) {
    $product_id = $product->get_id();
    $name       = $product->get_name();
    $price      = $product->get_price();
    $sku        = $product->get_sku();
    $url        = $product->get_permalink();
}

get_permalink() is also the answer to “how do I get the product URL.” For a variation it returns the parent product’s URL with the variation’s attributes added, like /product/var-tee/?attribute_color=Red, so the right options are preselected.

Always check the return value. wc_get_product() returns false when the ID doesn’t belong to a product (deleted, wrong post type, typo), and calling a method on false is a fatal error. It also returns false if you call it too early: WooCommerce’s source refuses to load products before the woocommerce_init action and its post type and taxonomy registration have run, so code in a plugin’s main file or on plugins_loaded needs to move to init or later.

By SKU: wc_get_product_id_by_sku()

If you have a SKU and need the product ID (common when syncing with an inventory system or importing data), WooCommerce ships a helper for it:

$product_id = wc_get_product_id_by_sku( 'TSHIRT-RED-LG' );

if ( $product_id ) {
    $product = wc_get_product( $product_id );
    // ...do something with the product
}

A few things about it that the WooCommerce code reference doesn’t spell out, all checked in the source and in testing:

  • It returns an integer: the ID, or 0 when nothing matches.
  • It searches variations too. A variation’s SKU returns the variation ID, so call get_parent_id() on the result if you need the parent.
  • It skips trashed products but does match drafts and private products.
  • It reads the wc_product_meta_lookup table, not post meta. If a SKU was written straight to the _sku meta key with update_post_meta() instead of $product->set_sku(), the lookup table never hears about it and the function returns 0. In my test, a SKU saved that way was not found.

Stores that use the GTIN, UPC, EAN or ISBN field have a matching helper, wc_get_product_id_by_global_unique_id(), available since WooCommerce 9.1.

From an Order’s Line Items

When you have an order and need the product IDs in it (for analytics, inventory updates, or fulfillment scripts):

$order = wc_get_order( $order_id );

if ( $order ) {
    foreach ( $order->get_items() as $item_id => $item ) {
        $product_id   = $item->get_product_id();   // parent ID for variations
        $variation_id = $item->get_variation_id(); // 0 if not a variation
        $quantity     = $item->get_quantity();
    }
}

For a variation, get_product_id() returns the parent variable product and get_variation_id() returns the exact variation. If you want the product object that was actually bought, $item->get_product() picks the variation when there is one.

This code is the same with or without High-Performance Order Storage (HPOS), which WooCommerce turns on by default for new stores since version 8.2. With HPOS on in my test, orders lived in the custom order tables and the line item methods returned the same IDs as on legacy storage.

From the Current Cart

To grab product IDs from whatever is in the customer’s cart (for upsells, conditional fees, or cart rules):

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $product_id   = $cart_item['product_id'];   // parent ID for variations
    $variation_id = $cart_item['variation_id']; // 0 if not a variation
    $quantity     = $cart_item['quantity'];
    $product      = $cart_item['data'];         // WC_Product (the variation, if any)
}

Same split as orders: product_id is always the parent, so check variation_id when you need to tell Red from Blue. WC()->cart is only loaded on front-end requests, so in admin or cron code it will be null.

All Product IDs (Bulk Query)

For a one-off script or migration that needs every product ID, use wc_get_products() with 'return' => 'ids':

$product_ids = wc_get_products( array(
    'status' => 'publish',
    'limit'  => -1,
    'return' => 'ids',
) );

Two defaults to know: without 'limit' => -1 it returns only as many products as your “Blog pages show at most” setting, and without 'status' it includes drafts, pending and private products. Variations are not included either way; get those from each parent’s get_children().

If you’d rather skip WooCommerce’s query layer, a plain get_posts() with 'fields' => 'ids' returns the same list without loading any product objects:

$product_ids = get_posts( array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'fields'         => 'ids',
) );

Product ID vs SKU: What’s the Difference?

WooCommerce also has a SKU field, and it is a different thing from the product ID.

SKU stands for Stock Keeping Unit. It’s an identifier you choose yourself for inventory or barcode management. The product ID is assigned automatically when the product is created; the SKU only exists if you fill it in.

SKU in WooCommerce

Not every product needs a SKU, and many stores leave the field empty. Every product always has an ID.


Can You Change a Product ID in WooCommerce?

No. There is no setting for it, because the ID is the row number WordPress gives the product in the wp_posts table. Orders, variations, reviews and product meta all point at that number, so editing it in the database breaks those links.

If you need a different identifier, you have two safe options:

  • Set a SKU and use it as your public product code. It can be changed any time.
  • Use the Duplicate link in the products list to make a copy. The copy gets a new ID (as a draft with no sales history), and you can trash the original. See how duplicating works in WordPress for what does and doesn’t carry over.

Frequently Asked Questions

Where is the WooCommerce product ID stored in the database?

It’s the ID column of the wp_posts table where post_type is product (or product_variation for variations). WooCommerce doesn’t have a separate products table for the ID; products are a WordPress custom post type.

Why does my variation ID differ from the parent product ID?

Each variation is stored as its own post with the product_variation post type, so it gets its own ID, and get_parent_id() points back at the variable product. In orders and the cart, get_product_id() and product_id return the parent, while get_variation_id() and variation_id return the specific variation.

Why does wc_get_product() return false?

Either the ID isn’t a product (deleted, wrong post type, or 0 because the variable holding it is empty), or the call ran before WooCommerce finished loading. It refuses to run before the woocommerce_init action, so move early code to the init hook or later.

Why does wc_get_product_id_by_sku() return 0?

Check that the SKU matches exactly and the product isn’t in the trash. If the SKU was saved directly to the _sku post meta key instead of through $product->set_sku() and save(), the wc_product_meta_lookup table the function reads was never updated, so it returns 0. Re-save the product through the WooCommerce API (or the edit screen) to fix it.

Does HPOS (High-Performance Order Storage) change how I get product IDs?

Not if you use the WooCommerce CRUD methods (wc_get_order(), $order->get_items(), $item->get_product_id()). They return the same values on both storage types. What breaks on HPOS stores is code that reads order data straight from wp_postmeta or uses get_post_meta() on an order ID.

Picture of Andy Feliciotti

Andy Feliciotti

Andy has been a full time WordPress developer for over 15 years. Through his years of experience has built 100s of sites and learned plenty of tricks along the way. Found this article helpful? Buy Me A Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Tips Monthly
Get the latest from SmartWP to your inbox.