A client of mine has a category called “Brand”. Each product is assigned to a specific Brand subcategory e.g. Nike, Adidas, etc. The goal of this task was to show the “Brand” subcategories in the shop/category/loop pages as a way to help the user identify the brand name.

WooCommerce Show Product SubCategories
WooCommerce Show Product SubCategories WooCommerce Show Product SubCategories

Show Product Subcategories on the WooCommerce Loop Pages


/**
 * @snippet       WooCommerce Show Product Subcategories
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17525
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.4.5
 */

add_action( 'woocommerce_after_shop_loop_item_title', 'bbloomer_show_all_subcats', 2 );
 
function bbloomer_show_all_subcats() {
 
global $post;
 
$cats = get_the_terms( $post->ID, 'product_cat' );
 
if ( ! empty( $cats ) ) {
	
        foreach ( $cats as $term ) {
 
	// If parent cat ID = 116 echo subcat name...
        if( $term->parent == 116 ) { 
		echo $term->name;
	}
 
        }
 
}
 
}

Snippet 2: Display Specific Category on the WooCommerce Shop Page


/**
 * @snippet       Display Specific Category ID Under Each Product Title @ Woo Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17525
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.4.5
 */

add_action( 'woocommerce_after_shop_loop_item_title','bbloomer_show_cat_id', 2 );

function bbloomer_show_cat_id() {
global $post;
$cats = get_the_terms( $post->ID, 'product_cat' );
if ( count($cats) > 1 ) {
	foreach ( $cats as $term ) {
		if( $term->term_id == 35 ) { echo $term->name; }
	}
}
}

Share:

Leave a Reply