Skip to content

Instantly share code, notes, and snippets.

@michaelhoang
Created April 27, 2015 07:55
Show Gist options
  • Save michaelhoang/962584cd4e5f1c210199 to your computer and use it in GitHub Desktop.
Save michaelhoang/962584cd4e5f1c210199 to your computer and use it in GitHub Desktop.
Magento Category
public function categoryTravel($category, $level)
{
$result = new Varien_Data_Collection;
if (is_numeric($category)) {
$category = Mage::getModel('catalog/category')->load($category);
}
$this->_categoryTravel($result, $level, $category);
return $result;
}
protected function _categoryTravel($result, $level, $category)
{
$result->addItem($category);
if ($level === null || $category->getLevel() < $level) {
$categoryChildren = $category->getChildrenCategories();
foreach ($categoryChildren as $_category) {
$this->_categoryTravel($result, $level, $_category);
}
}
}
// Use function
categoryTravel(5, 6); // Category id = 5, level = 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment