I not try this, but I hope this working
function get_list_catalog_category_product(){
//get all catalog
$catalogs = get_terms( array(
'taxonomy' => 'catalogs_term',
'hide_empty' => false
));
if ( !empty($catalogs) ) :
//make list catalog
$output="<ul>";
foreach( $catalogs as $catalog ) {
//write all catalog
$output.= '<li><a href="'.get_term_link( $catalog ).'">'.$catalog->name.'</a>';
//find all product related the catalog
$args = array(
'post_type' => 'product_post',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'catalogs_term',
'field' => 'term_id',
'terms' => $catalog->term_id
)
)
);
$products = get_posts( $args );
//make array of category related the catalog
$all_category = array();
//make array of product divided by category related the catalog
$product_by_cat = array();
if ( !empty($products) ){
foreach( $products as $product ) {
//write every product html
$products_html="<li><a href="".get_permalink($product->ID).'">'.$product->post_title.'</a></li>';
//find all category of this product
$product_categories = wp_get_object_terms( $product->ID, 'category' );
if ( ! empty( $product_categories ) ) {
foreach($product_categories as $product_category) {
//write the product html in related category if category never created this code will create or if has been create product html just append
if(isset($product_by_cat[$product_category->term_id])){
$product_by_cat[$product_category->term_id] .= $products_html;
}else{
$product_by_cat[$product_category->term_id] = $products_html;
}
//push the category as category related to catalog
array_push($all_category,$product_category->term_id);
}
}
//remove duplicate category if a product has more than 1 category
$all_category = array_unique($all_category);
}
}
if ( !empty($all_category) ){
$output.= '<ul>';
foreach( $all_category as $the_category ) {
//write category html in catalog
$output.='<li><a href="'.get_term_link($the_category).'">'.get_term( $term_id, 'category' )->name.'</a></li>';
//write all product html to the category
$output.='<ul>'.$product_by_cat[$the_category].'</ul>';
}
$output.= '</ul>';
}
$output.= '</li>';
}
$output.='</ul>';
echo $output;
endif;
}