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!
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: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: