Filter category in WooCommerce shop page to display related sub-category

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.

1 Answer
1

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' );

Leave a Comment