When customizing a WooCommerce store, you may want to show content, load a script, or change a layout only on an individual product page. WooCommerce provides a simple conditional function for this purpose: is_product(). It lets your theme or plugin check whether the current page displays a single product, so you can target product pages without affecting the shop, cart, or other parts of your site.
In this guide, you’ll learn how to use is_product(), where to place it, how it differs from related WooCommerce conditionals, and what to check if your code does not behave as expected. These examples are intended for a child theme or a custom plugin; avoid editing WooCommerce core files, since updates can overwrite your changes.
What Does is_product() Do in WooCommerce?
is_product() is a WooCommerce conditional tag that returns true when the main query is displaying a single product page. It returns false on pages such as the shop archive, product category archive, cart, checkout, and blog posts.
A basic PHP check looks like this:
if ( function_exists( 'is_product' ) && is_product() ) {
// Run code only on a single product page.
}
The function_exists() check is optional when WooCommerce is guaranteed to be active, but it can help prevent a fatal error if your code runs on a site where WooCommerce is disabled. The conditional itself should generally run after WordPress has set up the main query, such as inside a suitable template or a hook that fires during page rendering.
Show Content Only on a Product Page
A common use for is_product() is displaying a short message, promotional notice, or delivery detail on every product page. You can add the following example to your child theme’s functions.php file or, preferably, to a small site-specific plugin:
add_action( 'woocommerce_single_product_summary', 'my_product_page_notice', 25 );
function my_product_page_notice() {
if ( ! function_exists( 'is_product' ) || ! is_product() ) {
return;
}
echo '<p class="product-page-notice">Free shipping on eligible orders.</p>';
}
The woocommerce_single_product_summary hook places the notice in the product summary area. Its priority, 25, determines its position relative to other functions attached to that hook. Lower priority numbers usually run earlier; higher numbers run later. You can adjust the priority to move your message, but the final position also depends on your theme and any customizations.
WooCommerce hooks that begin with woocommerce_single_product_ are intended for the single-product layout. Because those hooks are normally called only by the product template, a separate conditional may be unnecessary in this exact example. Keeping the check can still make the callback’s intention explicit and can protect it if you later reuse the function elsewhere.
Load CSS or JavaScript Only on Product Pages
Conditional loading can help avoid adding product-specific assets to every page on your store. Use WordPress’s enqueue functions rather than printing a script or stylesheet directly into the page:
add_action( 'wp_enqueue_scripts', 'my_product_page_assets' );
function my_product_page_assets() {
if ( ! function_exists( 'is_product' ) || ! is_product() ) {
return;
}
wp_enqueue_style(
'my-product-page',
get_stylesheet_directory_uri() . '/css/product-page.css',
array(),
'1.0.0'
);
wp_enqueue_script(
'my-product-page',
get_stylesheet_directory_uri() . '/js/product-page.js',
array(),
'1.0.0',
true
);
}
Place the files in the paths shown, or update the paths to match your child theme. The final true argument requests that the JavaScript load in the footer. WordPress will then enqueue these assets only when the main query is a single product page.
For larger stores, consider whether the asset is truly needed on every product. You can add extra checks for a specific product type or product ID, or use a product-specific class in your stylesheet if you only need to alter presentation.
Use is_product() in a Theme Template
You can also use the conditional directly in a PHP template when you need different markup on product pages. For example:
<?php if ( function_exists( 'is_product' ) && is_product() ) : ?>
<div class="product-support-message">
Contact us if you need help choosing this item.
</div>
<?php endif; ?>
In a classic theme, this code can go in an appropriate template or template part, ideally within a child theme. However, a WooCommerce hook is often easier to maintain because it avoids copying an entire template file. If you do override a WooCommerce template, check the template version and review the override after updates.
Block themes and the WooCommerce block-based product templates may not use the same PHP template structure as a classic theme. In those setups, use the available blocks, patterns, or supported extension points where possible. The conditional tag remains useful in PHP code, but it cannot directly control content inside a block editor layout unless that content is rendered through compatible PHP.
Check for a Specific Product
is_product() answers whether the visitor is viewing any single product. It does not accept a product ID as an argument. To target one particular product, first check that the page is a product page, then compare the current product’s ID:
if ( function_exists( 'is_product' ) && is_product() ) {
$product_id = get_queried_object_id();
if ( 123 === (int) $product_id ) {
// Run code only for product ID 123.
}
}
Replace 123 with the product ID you want to target. Using get_queried_object_id() reads the ID for the object represented by the main query. Casting the result to an integer and using a strict comparison helps make the check predictable.
If you need product data rather than just the ID, WooCommerce’s product object is usually the better choice:
if ( function_exists( 'is_product' ) && is_product() ) {
global $product;
if ( $product instanceof WC_Product ) {
$product_id = $product->get_id();
$product_type = $product->get_type();
}
}
Use WooCommerce product methods when available instead of reading product metadata directly. Product objects provide a more reliable way to access information such as the product ID, type, price, and stock status.
is_product() Compared with Other WooCommerce Conditionals
WooCommerce offers several conditional tags that describe different page contexts. Choosing the right one prevents content from appearing in the wrong place:
-
is_product()checks for a single product page. -
is_shop()checks for the main shop archive. -
is_product_category()checks for a product category archive. -
is_product_tag()checks for a product tag archive. -
is_cart()checks for the cart page. -
is_checkout()checks for the checkout page. -
is_woocommerce()checks for several WooCommerce pages and archives, but not every page that happens to contain a WooCommerce shortcode or block.
For example, use is_shop() when a banner belongs above the product catalog, not on individual products. Use is_product_category() for category-specific archive messaging. If you combine conditionals, make the intended scope clear:
if ( function_exists( 'is_product' ) && is_product() ) {
// Single product page.
} elseif ( function_exists( 'is_shop' ) && is_shop() ) {
// Main shop archive.
}
Common Mistakes to Avoid
One frequent mistake is calling is_product() too early. Conditional tags depend on the main WordPress query, so they may not return the expected result before that query has been prepared. Avoid placing the check at the top level of a plugin file or running it during very early initialization. Put it inside a rendering callback, a suitable WordPress hook, or a template that runs after the query is available.
Another mistake is confusing a single product page with any page that contains a product. A product block, shortcode, or custom query can display products on a regular page, but that does not make the page a single product page. In that case, is_product() correctly returns false. Use the context of the block, shortcode, or custom query instead.
Also avoid placing PHP code directly into a WordPress page or product description unless your site has a secure, purpose-built method for processing it. Use a theme, plugin, or supported editor feature. Editing the parent theme’s functions.php is another risk: a theme update can remove your changes. A child theme or custom plugin is safer.
Finally, do not use is_product() as a substitute for security or permission checks. It identifies the current page type; it does not validate user input or decide whether a visitor is authorized to perform an action. Sanitize and validate input, escape output, and use WordPress capability checks whenever the code handles sensitive data or privileged operations.
Troubleshooting When the Conditional Does Not Work
If your code does not run where expected, confirm that WooCommerce is active and that you are testing a real single product URL rather than the shop archive or a custom page with a product block. Then check that the code is executing after the main query is available.
For temporary debugging, you can log the result from a suitable hook:
add_action( 'wp', function() {
if ( function_exists( 'is_product' ) ) {
error_log( 'Is product page: ' . ( is_product() ? 'yes' : 'no' ) );
}
} );
Check your PHP error log rather than displaying debug messages to shoppers. Remove the debugging code when you finish testing. If a theme or plugin changes the product template, inspect its hooks and template structure; the conditional may be correct even when the callback is never reached.
Caching can also make a change appear not to work. Clear your site, server, and browser caches after updating code or assets. For CSS and JavaScript, update the version argument in wp_enqueue_style() or wp_enqueue_script() when appropriate, so browsers can request the latest file.
Conclusion
To check whether the current page is a WooCommerce single product page, use is_product(). It is useful for displaying product-only messages, enqueuing targeted assets, and applying conditional template logic. Place the check where the main query is available, use the appropriate conditional for shop and archive pages, and add product ID checks when you need to target an individual item.
For maintainable WooCommerce customizations, put code in a child theme or custom plugin, use hooks where practical, and avoid changing core files. With the right conditional and a suitable hook, you can tailor the product-page experience without unintentionally changing the rest of your store.