0
votes

I am implementing the Magento catalog product view page, and I need the name of subcategory the product is added to.

The code to display the category is like: $_product->category->name

but I am unable to get the sub category name.

2

2 Answers

0
votes

to get the subcategory of a category, use the below code

$your_category_id = '2334'; // category_id whose subcategory you want to fetch
$subcats = Mage::getModel('catalog/category')->getCategories($your_category_id);
foreach ($subcats as $sub) {
    echo $sub->getName();
}
0
votes

Well I got the sub categories the following way:

<?php
if (Mage::registry('current_product')) {
    if ($_product) {
        $categoryIds = $_product->getCategoryIds();            
        $cat_id = $categoryIds[0]; ----> pass the level of sub catgeory to the array index
        $category = Mage::getModel('catalog/category')->load($cat_id);
        $cat_name = $category->getName();
    }
}
?>