How To Get All Categories Of A Product In Magento.?

Get all category products in Magento :

Sometimes we need to get all Magento categories name with URL of a product.  Below code will get you all the categories with URL of product.Product can be shown under more than one category, so you may get more than one category ID.


First Method:- 

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

Second Method:- This is a more better way than the above code for programmatically.

$categoryIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
                     //->addAttributeToSelect('name')
                     //->addAttributeToSelect('url')
                     ->addAttributeToSelect('*')
                     ->addAttributeToFilter('entity_id', $categoryIds)
                     ->addIsActiveFilter();

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

Hope this helps. Thanks.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *