Skip to content

Instantly share code, notes, and snippets.

@justindocanto
Created June 1, 2015 21:55
Show Gist options
  • Save justindocanto/35cd134a15246e1f40ac to your computer and use it in GitHub Desktop.
Save justindocanto/35cd134a15246e1f40ac to your computer and use it in GitHub Desktop.
Magento - Return id, name & url of product categories
<?php
// Get Magento categories
function get_magento_categories() {
require_once 'app/Mage.php';
Mage::app();
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$categories = array();
if ($ids){
$i=1;
foreach ($ids as $id){
$cat = Mage::getModel('catalog/category');
$cat->load($id);
if($cat->getName() !== "Root Catalog" && $cat->getName() !== "Store") { // My lazy way of hiding root categories
$categories[$i]['id'] = $id;
$categories[$i]['name'] = $cat->getName();
$categories[$i]['url'] = $cat->getUrl();
$i++;
}
}
}
return $categories;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment