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

function hwn_filter_content_using_issingle($content) {
    if( is_single(35) && is_main_query() ) {
        $new_content = '<p>This bonus content is added to the bottom of post 35.</p>';
        $content .= $new_content;   
    return $content;
add_filter('the_content', 'hwn_filter_content_using_issingle');

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.

add_action( 'woocommerce_before_single_product', 'hwn_use_issingle_to_add_content_to_a_single_product_page' );

function hwn_use_issingle_to_add_content_to_a_single_product_page() {
   if ( is_single( 15 ) ) {
      echo "<p>This content has been added because the product id is equal to 15.</p>";
   if ( is_single( 'belt' ) ) {
      echo "<p>This content has been added because the product name is equal to 'belt'.</p>";
   if ( is_single( 'Belt' ) ) {
      echo "<p>This content has been added because the product title is equal to 'Belt'.</p>";

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

add_action( 'woocommerce_before_single_product', 'hwn_add_some_custom_content_for_product_44' );
function bbloomer_run_this_for_specific_product_id() {
    global $product;
    if ( $product->get_id() == 44 ) {
       echo '<p>Product 44 is on sale today please don\'t miss this once in a lifetime opportunity.</p>';

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

add_action( 'woocommerce_before_single_product', 'hwn_add_some_custom_content_for_all_products' );
function bbloomer_run_this_on_all_product_pages() {
    global $product;
    if ( is_product() ) {
       echo '<p>All our stock is kept in a secure, clean warehouse, if you place an order today we are ready to ship to you in seconds!</p>';

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

add_action( 'woocommerce_before_single_product', 'hwn_use_issingle_to_add_content_to_multiple_product_pages' );

function hwn_use_issingle_to_add_content_to_multiple_product_pages() {
    global $post;
    if ( is_single( array(15, 'T-Shirt with Logo', 'sunglasses' ) )) {
        echo "<p>This content has been added because the product has a property that matches one of the arguments passed to the 'is_single' function.</p>";

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

add_action( 'woocommerce_before_single_product', 'hwn_add_some_custom_content_for_product_three_different_products', 20 );
function hwn_add_some_custom_content_for_product_three_different_products() {
    global $product;

    // Add the product ids you want to dsiplay the content for to the array below 
    $targeted_product_ids = array( 33, 44, 55 );

    if( in_array( $product->get_id(), $targeted_product_ids ) ) {
        echo 'Limited stock, please purchase todayto avoid disappointment';

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.


Warning: Undefined array key "format" in H:\root\home\bravenewworld101-001\www\hardworkingnerd\wp-content\plugins\convertkit\includes\class-convertkit-resource-forms.php on line 153

Leave a Comment