Skip to content

Instantly share code, notes, and snippets.

@code-poel
Last active August 29, 2015 14:05
Show Gist options
  • Save code-poel/55a364aa8832600bece2 to your computer and use it in GitHub Desktop.
Save code-poel/55a364aa8832600bece2 to your computer and use it in GitHub Desktop.
Get all active category names.
<?php
public function getAllCategoryNames()
{
$names = array();
// Build out a category collection and add the name attribute to the select.
$model = Mage::getModel('catalog/category');
$collection = $model->getCollection();
$collection->addAttributeToSelect('name');
// Make sure the categories are actually active.
$collection->addIsActiveFilter();
// Start at the root.
$collection->addLevelFilter(1);
// Order by name (obviously, optional).
$collection->addOrderField('name');
foreach ($collection as $category) {
/* @var $category Mage_Catalog_Model_Category */
$names[] = $category->getName();
}
return $names;
}
public function getAllCategoryNamesRespectDepth()
{
$names = array();
$helper = Mage::helper('catalog/category');
// Watch out, this method respects the depth limit set in the
// 'catalog/navigation/max_depth' setting in admin.
$collection = $helper->getStoreCategories('name', true, false);
foreach ($collection as $category) {
/* @var $category Mage_Catalog_Model_Category */
$names[] = $category->getName();
}
return $names;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment