Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Might be because your first statement is missing a closing )? It should be if (is_product_category('audio')) –  stealthyninja Dec 12 '12 at 20:32
    
Good catch, but that's not it. is_product_category doesn't seem to work on single-product.php –  Alex Dec 12 '12 at 22:10

3 Answers 3

up vote 3 down vote accepted

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.

share|improve this answer
    
Thank you so much, Justin! –  Alex Dec 18 '12 at 21:47

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';
}
share|improve this answer

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.

share|improve this answer
    
I am not able to code this. I don't have a clue how to get this to work. Somebody please illustrate this. I tried my best up there. Should I replace this with get_categories()? –  Alex Dec 13 '12 at 16:04
    
@Alex the is_product_category() function returns TRUE if you're on the product category page. Not the category of the product. I'm heads down on a project right now, but I'll try to get you a code snippet later. –  Stephen Dec 13 '12 at 17:55
    
Thanks, Steven for taking time to code this little snippet. Appreciate it very much. –  Alex Dec 13 '12 at 17:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.