Skip to content

Instantly share code, notes, and snippets.

@arioch1984
Created October 24, 2014 10:08
Show Gist options
  • Save arioch1984/67cc0e7a6deb916439d7 to your computer and use it in GitHub Desktop.
Save arioch1984/67cc0e7a6deb916439d7 to your computer and use it in GitHub Desktop.
get an array of all categories with details in Magento
<?php
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('url_key')
->addAttributeToSelect('url')
->addAttributeToFilter('level',2)
->addAttributeToSelect('is_active');
foreach ($categories as $category)
{
if ($category->getIsActive()) { // Only pull Active categories
$entity_id = $category->getId();
$name = $category->getName();
$url_key = $category->getUrlKey();
$url_path = $category->getUrl();
}
}
//OR
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
if ($ids){
foreach ($ids as $id){
$cat = Mage::getModel('catalog/category');
$cat->load($id);
$entity_id = $cat->getId();
$name = $cat->getName();
$url_key = $cat->getUrlKey();
$url_path = $cat->getUrlPath();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment