How to check if the product is in a certain category on a single-product.php in Woocommerce?

The question:

How in the world can I check if a product is in a certain product category on the single-product.php?

<?php if (is_product_category('audio')) {
           echo 'In audio';
               woocommerce_get_template_part( 'content', 'single-product' );

      } elseif (is_product_category('elektro')) {

            echo 'In elektro';
            woocommerce_get_template_part( 'content', 'single-product' );
         } else {
            echo 'some blabla'; }  ?>

is_product_category(‘slug’) does not have an effect on the single-product.php. I want to have the upper conditionals. Any solution for this on a single-product page?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I don’t think get_categories() is the best option for you in this case because it returns a string with all the categories listed as anchor tags, fine for displaying, but not great for figuring out in code what the categories are. Ok, so the first thing you need to do is grab the product/post object for the current page if you don’t already have it:

global $post;

Then you can get the product category term objects (the categories) for the product. Here I’m turning the category term objects into a simple array named $categories so it’s easier to see what slugs are assigned. Note that this will return all categories assigned to the product, not just the one of the current page, ie if we’re on /shop/audio/funzo/:

$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

Then we just have to check whether a category is in the list:

if ( in_array( 'audio', $categories ) ) {  // do something

Putting it all together:

<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;

if ( in_array( 'audio', $categories ) ) {
  echo 'In audio';
  woocommerce_get_template_part( 'content', 'single-product' );
} elseif ( in_array( 'elektro', $categories ) ) {
  echo 'In elektro';
  woocommerce_get_template_part( 'content', 'single-product' );
} else {
  echo 'some blabla';
}

Hopefully this is what you were looking for and answers your question.

Method 2

has_term should work in this case:

if ( has_term( 'audio', 'product_cat' ) ) {

       echo 'In audio';
       woocommerce_get_template_part( 'content', 'single-product' );

} elseif ( has_term( 'elektro', 'product_cat' ) ) {

       echo 'In elektro';
       woocommerce_get_template_part( 'content', 'single-product' );

} else {
       echo 'some blabla';
}

Method 3

It’s worth noting that you can go through a list of options by calling an array rather than having to clutter up your code with lots of elseif checks, assuming you want to do the same thing with each category that is.

if( has_term( array( 'laptop', 'fridge', 'hats', 'magic wand' ), 'product_cat' ) ) :

// Do stuff here

else :

// Do some other stuff

endif;

Method 4

This is old but just in case people are still looking WooThemes as a simple solution:

if ( is_product() && has_term( 'your_category', 'product_cat' ) ) {
    //do code
}

*Change ‘your_category’ to whatever you are using.

Here is the link to the documentation: https://docs.woothemes.com/document/remov-product-content-based-on-category/

Method 5

I’d look at using the get_categories() function of the WC_Product class.

You can find the link to the documentation here.

Basically within the loop of the page call the function to return the categories associated with the product.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment