4

I'm a newbie at modifying wooCommerce. I hope someone can help me with a problem i'm having while trying to make a minor change to the plugin to fit with a request from my client.

I'm trying to modify WooCommerce to display the category name for each product. I want the category name to be shown above the product image on the main shop page. (the view where many products are shown on the screen at once)

I've searched around, and i can't seem to find how to call the product->category to get the category name for each product being shown.

In the wooCommerce files, i've found content-product.php
(in templates/content-product.php)

and i'd like to inject the category for each product by adding to this action: do_action( 'woocommerce_before_shop_loop_item_title' );

I understand that I can add a function in my theme's functions.php to inject some new code into the ''woocommerce_before_shop_loop_item_title'

but I can't work out how to get the category for each product.

can someone help? or tell me where i might be able to find the answer?

any help will be much appreciated, thank you!

1
  • Why this question is off topic, XD.....
    – Brethlosze
    Jun 7, 2016 at 3:47

2 Answers 2

15

The product title is added to the woocommerce_single_product_summary hook at priority 5, so you'll want to add to the same hook with a lower (earlier) priority. You'd add this to your theme's functions.php file. I presume when you say "category" you mean the WooCommerce product category, so this should return the first one and print it before the product title:

function wpa89819_wc_single_product(){

    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){

        $single_cat = array_shift( $product_cats ); ?>

        <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>

<?php }
}
add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );
5
  • Thanks so much! your solution worked great and yes, you guessed right that I meant the WooCommerce product category, not the wordpress page category.
    – user28546
    Mar 18, 2013 at 22:48
  • Great! If it worked and solved your question can you select it as the answer? Mar 18, 2013 at 23:09
  • hi helgatheviking, Thanks again for your help. sorry I'm a newbie and i can't see how to mark this as answered?!?
    – user28546
    Mar 26, 2013 at 19:21
  • There should be a check mark next to all the answers. When you click on it it will turn green to signify that this is the correct answer. Choosing answers keeps your percentage up so people will be more likely to help you in the future. Please read the "how do I ask questions" part of the FAQ Mar 27, 2013 at 8:52
  • Thanks for this solution. But I have a product category and inside a sub product category. When I add the code above only the sub product category is printed. How can I print the first parent of this tree?
    – codajoao
    Jun 20, 2015 at 3:26
3

you need the get_categories function, which takes the following arguments:

get_categories( $separator, $before, $after )

so in it's simples form (assuming only one category per product), just add <?php echo $product->get_categories(); ?> to your content-product.php (line 53). If you have more categories, you can do it like on the single product page:

<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
2
  • note that if you want to display the title above the thumbnail, you should put it in line 43, but this complicates things as any 'sale' tags etc. could stay in place (instead of moving with the thumbnail).
    – Ewout
    Mar 10, 2013 at 12:43
  • I had to use $_product->get_categories (with the underscore in $_product) and it worked great.Thanks!
    – Peanuts
    Feb 9, 2017 at 13:19

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