Using is_single() in WooCommerce

If you’ve ever done any coding in WordPress then you may have used the is_single() function, it’s super useful for finding out if you’re on a specific single post page

If you’re moving from WordPress to WooCommerce then you can use is_single, in the same way, to ensure that content is only displayed for a specific product.

The example below shows the different ways you can use “is_single()” to add some content to a specific product page, in this case, a page selling a belt.

This will give us a result similar to the one below

Alternatives to Using is_single()

As an alternative to using “is_single()” we can do something like this

Here we use the global $product variable and the call the get_id() function on the product object to check we have the correct id, our content is then shown on the correct product page as below.

Does WooCommerce have an Equivalent to is_single()?

The answer to this is “sort of”, WooCommerce does provide an “is_product” function but unlike “is_single” you are unable to supply an id as an argument to the function. So whilst you can’t target a specific product as in our example above using “is_product()” you can do something like this

What if I want to Show Some Content on Multiple Specific Product Pages?

So far, we’ve shown how to target a specific product and all products, but what if we want to add some content to two or three specific products in our store, well, “is_single()” lets us do that as well

Note how we can pass different search terms into an array and “is_single” will be able to perform a match against the relevant product. In the example above we passed in a product, a product title, and a product name.

As before it also possible to do a similar thing using the global $product variable and a call to the get_id() function on the product object

In this example, we use an array and php’s built-in “in_array” function to check if we’re on one of the pages that we wish to add the content to. Note that by using this method we are only able to search using product ids.


Leave a Comment