Magento get all categories of a product

· kalpesh

Get all the categories a product belongs to in Magento. Below code will get you all the categories with details the product is attached to. Product can be shown under more than one category, so you may get more than one category ID. Either get the category collection from product, or get all the category IDs and then load them using catalog category collection model.

//$_product = Mage::getModel(‘catalog/product’)->load($productID);

First way,

$catCollection = $_product->getCategoryCollection();  
foreach($catCollection as $cat){  
 print_r($cat->getData());  
 //echo $cat->getName();  
 //echo $cat->getUrl();  
}

Another way,

$catIds = $_product->getCategoryIds();  
$catCollection = Mage::getResourceModel(‘catalog/category_collection’)  
 //->addAttributeToSelect(‘name’)  
 //->addAttributeToSelect(‘url’)  
 ->addAttributeToSelect(‘*’)  
 ->addAttributeToFilter(‘entity_id’, $catIds)  
 ->addIsActiveFilter();

foreach($catCollection as $cat){  
 print_r($cat->getData());  
 //echo $cat->getName();  
 //echo $cat->getUrl();  
}

#category #magento #product