I need to find a solution to get product current subcategory from specific parent category.
For example: I have a product that has like 2 parent categories like authors and publishers. And in those i have like 500-800 subcategories, and this product has one of subcategories in each parent categories selected. I need to get those subcategory names. My solution:
function getSubsName($_product,$cat_id)
{
$categories = $_product->getCategoryIds();
$cat = Mage::getModel('catalog/category')->load($cat_id);
$subcats = $cat->getChildren();
$content = "";
foreach(explode(',',$subcats) as $subCatid):
$_category = Mage::getModel('catalog/category')->load($subCatid);
foreach($categories as $cat_id):
if($cat_id == $_category->getId()):
$content = '<a href="' . $_category->getUrl() .'">' . $_category->getName() . '</a>';
endif;
endforeach;
endforeach;
return $content;
}
But there is a problem because it loads very long. Like 8-10 seconds or more. I can't afford that. Anyone know any more simple and fast solution for this?
if($cat_id == $_category->getId()):
? You'll only get the category for which you gave the ID as an argument in the method