How to retrieve WooCommerce item ID in WordPress

I’m working on custom WordPress functionality with WooCommerce and need help getting the current item ID.

I’m using Advanced Custom Fields (ACF) to add extra fields to my WooCommerce items. ACF needs the item ID to fetch these custom fields properly.

The problem is I can’t seem to get the item ID when viewing an item page. I’ve been trying to output the ID in my functions.php file but keep getting zero or false results.

Here are the methods I’ve attempted:

$current_id = get_queried_object_id();
var_dump($current_id);
$item_id = get_the_ID();
var_dump($item_id);
global $wc_product;
$id = $wc_product->get_id();
var_dump($id);

All these approaches return either 0 or boolean false. My item pages have URLs like: mysite/product/sample-item/

I’m running WordPress 6.3.2 with WooCommerce 8.2.0. What’s the correct way to get the current item ID on these pages so I can use it with ACF?

This happens because functions.php runs before WooCommerce loads the product data. I ran into the same thing when I was setting up ACF with custom product fields a few months back. Don’t put your code straight in functions.php - hook into the right action instead. The wp hook fixed it for me. It fires after WordPress loads but before headers get sent:

function get_woo_product_id() {
    if (is_product()) {
        global $post;
        $product_id = $post->ID;
        // Now you can use this with ACF
        $custom_field = get_field('your_field_name', $product_id);
    }
}
add_action('wp', 'get_woo_product_id');

You could also try get_queried_object_id() with the same hook setup. Just make sure WooCommerce has the product context ready before grabbing the ID. This method’s been solid for me across different WooCommerce versions.

had the same issue a few months ago. you’re using the wrong global variable - it should be global $product; not global $wc_product;. if globals aren’t working, try the wc_get_product() function instead. sometimes the product object hasn’t loaded when your code runs, so wrap it in a conditional check first.

This sounds like a timing issue - your code’s probably running before WooCommerce fully loads the product object. I ran into the same thing last year building custom product displays. Try the woocommerce_single_product_summary hook, or check if your code’s firing too early in WordPress. What worked for me was using the global $product object:

 global $product; 
 if ($product) { 
    $product_id = $product->get_id(); 
    var_dump($product_id); 
 } 

Don’t call this directly in functions.php - make sure you’re in the product page context. I wrap mine in is_product() so it only runs on single product pages. WooCommerce’s timing with globals is finicky, so getting the right hook is key.