Advertisement
lorro

WooCommerce - Insert category name(s) before product

Jul 29th, 2015
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <?php
  2.   // insert category name(s) before product in shop loop
  3.   // products can have none, one or many categories
  4.   // code goes in functions.php for your child theme
  5.   add_action('woocommerce_before_shop_loop_item', 'insert_cat_names');
  6.   function insert_cat_names() {
  7.     $terms = get_the_terms($product->ID, 'product_cat');
  8.     $nr_terms = count($terms);
  9.     if ($nr_terms == 1) {
  10.       print '<p class="cat_names">Category: '.$terms[0]->name.'</p>';
  11.     }
  12.     if ($nr_terms > 1) {
  13.       print '<p class="cat_names">Categories: ';
  14.       $first = true;
  15.       foreach ($terms as $term) {
  16.         if ($first) {
  17.           $first = false;
  18.         } else {
  19.           print ', ';
  20.         }
  21.         print $term->name;
  22.       }
  23.       print '</p>';
  24.     }
  25.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement