1

I am working with product category filtering with no success. I am attempting to pull the sub-category for products to display in between the product title and price on all products when viewed in the shop page.

I've tried a few solutions and am really struggling. Below is the link to the shop page where I am trying to display each sub-category of the following Category: Designer -> Designer Name.

I need to put Designer Name sub-category in between the Title and Price here: http://glamboutique.wpengine.com/shop/

I've made some progress. I currently have the parent category showing, but like I stated, I just need the sub-category of the Designer (parent category) to show in between the Title and Price. Below is the code:

function wc_shop_product_sub_category() {
    $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_after_shop_loop_item_title', 'wc_shop_product_sub_category' );


Update

So after reviewing, I came up with the solution with Fayaz's response as a solid start:

function wc_shop_product_sub_category() {
    global $post;
    // grabs a specific category based on the slug
    $brands_id = get_term_by('slug', 'designers', 'product_cat');

    // gets subcats and loops through them to display
    $terms = get_the_terms($post->ID, 'product_cat');
    foreach ($terms as $term) {
        if($term->parent === $brands_id->term_id) { ?>
            <h6 class="product-sub-cats"><?php echo $term->name; ?></h6>
            <?php  break;
         }
    }
}
// this will place the designers between title & price
add_action( 'woocommerce_shop_loop_item_title', 'wc_shop_product_sub_category' );

Currently this solution does not place the Designer Name in between the Title and Price, looking into how to remedy that.

  • Please edit your question and show us the work / CODE you've tried so far. – Fayaz Mar 8 '17 at 16:14
  • @Fayaz Sorry I made some progress and added the code. I am now at the point where I need to pull a specific sub category from the Designer parent. – Erik Mar 8 '17 at 17:02
  • I added this to my functions.php file. I am wondering if it might be best to modify the content-product.php files instead. Would that give me better control on where I could place the Sub-Category? – Erik Mar 8 '17 at 17:08
  • It may or may not depending on your theme CODE. However, best place for add_action is the functions.php file, not the template files. – Fayaz Mar 8 '17 at 20:05
  • @Fayaz yea I placed it in functions. Any idea on how to get that sub category, I've looked through all the documentation and can't find anything. – Erik Mar 8 '17 at 20:58
0

According to your description I'm assuming your each product has a category named Designers and designer-name subcategory under Designers category.

In that case the following CODE will work for you (read the comments within the CODE for explanation):

function wc_shop_product_sub_category() {
    // get the category object for "designers" category
    $designers_category = get_term_by( 'name', 'designers', 'product_cat' );
    if( $designers_category ) {
        // this CODE returns all the sub-categories of "designers" category
        // that belongs to the current product in the loop
        $designers = wp_get_post_terms( get_the_ID(), 'product_cat', array( 'child_of' => $designers_category->term_id ) );
        if ( $designers && ! is_wp_error( $designers ) ) {
            // remember, there may be multiple sub categories for "Designers" category. So if you want to
            // show multiple designers, you'll need to loop through the $designers array to show them all
            $designer = array_shift( $designers );
            ?>
            <h2 itemprop="name" class="product_category_title"><span><?php echo $designer->name; ?></span></h2>
            <?php
        }
    }
}
// this will place the designers between title & price
add_action( 'woocommerce_shop_loop_item_title', 'wc_shop_product_sub_category' );
// this will place the designers after price
// add_action( 'woocommerce_after_shop_loop_item_title', 'wc_shop_product_sub_category' );
  • So I went ahead and plugged that in, but it still does not work. I dug a bit and realized that a product may have multiple parents, 2-3 to be exact. So I disabled one, to leave only the Designer checked, and once again it did not work. It did display the Designer category but not its Designer Sub Category. I'm looking into some ways to get this to work let you know what modifications I make. Thanks! – Erik Mar 8 '17 at 23:48
  • I updated my code to a working version. I copied it to my main post. Your code definitely helped me find a solution. It is currently displaying it properly but now I have the issue of it not putting it in between the Title and the Price. Looking into this as we speak. – Erik Mar 9 '17 at 0:02
  • thanks, works perfect. Still having issues getting that to come up in between the title and price. Contacted the theme developer, think they modified in the code, having trouble finding where so figure I just ask them. Thanks for your help! – Erik Mar 9 '17 at 1:38
  • BTW, did you clear all the cache (browser cache, server/plugin cache etc.) while checking the results? Sometimes cache may play with the output :) – Fayaz Mar 9 '17 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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