If you’re building custom WooCommerce plugins or working on client projects where WooCommerce is involved, you’ve probably used wc_get_product()
at some point — only to see it return false
when you expected a product object.
This usually happens at the worst possible time — during a tight deadline or while debugging a critical feature. So why does this happen? And more importantly, how can you fix it quickly?
Let’s break down the most common reasons WooCommerce’s wc_get_product()
returns false
, and go over some smart ways to debug product data in your custom code.
But when WooCommerce can’t find the product — or isn’t ready yet — you get false
.
Why wc_get_product() Returns False
Here are the most common reasons:
- Invalid or missing product ID
You passed an ID that doesn’t exist or isn’t a product. - WooCommerce isn’t fully initialized
If you callwc_get_product()
too early (like duringplugins_loaded
), it won’t work because the product post type isn't registered yet. - Product is in the trash or draft
Some post statuses prevent the product from being retrieved normally. - WooCommerce isn’t active
If your code runs but WooCommerce is disabled (or not installed),wc_get_product()
doesn’t exist — and returns nothing. - Product post type hasn’t been registered
If WooCommerce’s init process hasn’t completed yet, theproduct
post type won’t be recognized, and nothing will load.
Don’t Call wc_get_product() Too Early
Timing matters a lot when you’re working with WooCommerce data.
Avoid calling wc_get_product()
in hooks like:
add_action('plugins_loaded', 'my_func'); // too early
Better options:
add_action('init', 'my_func'); // okay for most cases
add_action('woocommerce_after_register_post_type', 'my_func'); // best choice
That last one, woocommerce_after_register_post_type
, ensures that the product
post type is already registered, which is crucial.
Quick Debug Plugin for WooCommerce Product Data
Here’s a plugin I use in dev environments when I need to inspect product data. Hit a URL like ?dbg_get_product=123
and view the raw object and meta.
Usage
Simply log in as an admin or Store Manager and visit: yoursite.com/?dbg_get_product=123
Full plugin code:
<?php
/**
* Plugin Name: 000 Dbg: Display WooCommerce Product Data
* Description: this is for debugging purposes only!! Pass ?dbg_get_product=PROD_ID to display product data.
* Version: 1.0.0
* Author: Svetoslav Marinov (Slavi) | Orbisius
*/
// load later
// https://github.com/woocommerce/woocommerce/issues/16395
//add_action('plugins_loaded', 'dbg_get_product'); // too early
add_action('init', 'dbg_get_product'); // ok but maybe too early
//add_action('woocommerce_after_register_post_type', 'dbg_get_product'); // better
function dbg_get_product()
{
if (empty($_REQUEST['dbg_get_product'])) {
return;
}
if (!function_exists('wc_get_product')) {
wp_die("wc_get_product doesn't exist. Is WooCommerce installed & activated?");
}
// Make sure you're logged in
if (!is_user_logged_in()) {
wp_die("Please login");
}
// current_user_can -> use if wp_get_current_user
if (!function_exists('wp_get_current_user') || !current_user_can('manage_woocommerce')) {
wp_die("Can you manage WooCommerce?");
}
// pass ?render_plain_text=0 to turn it off
$prod_id = (int) $_REQUEST['dbg_get_product'];
$product = wc_get_product($prod_id);
$dbg = '';
if (empty($product)) {
$dbg = 'not found';
} else {
$dbg .= "Data: ";
$dbg .= print_r($product, true);
$meta_data = $product->get_meta_data();
if (!empty($meta_data)) {
$dbg2 = print_r($meta_data, true);
$dbg .= "\n\nMeta Data: " . $dbg2;
}
}
// We don't need escaping because it's plain text!
header('Content-type: text/plain');
echo $dbg;
exit;
}
Quick Fix Checklist
- Hook into
woocommerce_after_register_post_type
- Confirm WooCommerce is active
- Validate the product ID
- Ensure product is published (not trashed/draft)
- Use capability checks
- Don’t call functions too early
Final Thoughts
If wc_get_product()
returns false, it's almost always due to timing or invalid input. The good news is: once you know when WooCommerce is ready, you can safely pull product data.
This kind of debugging is a regular part of WooCommerce plugin development — especially when you're extending it in custom ways. Having tools like this simple debug plugin can save hours of hair-pulling when you're not sure why something isn't loading.