点击这里 快速注册 shopify 英文网店 可以获得14天免费试用,注册完成后 您可以设置您的网站,添加您产品,快速预览你的网店 , 开启国际电子商务之旅注册时注意以下几点 1…
March 12, 2016
Magento: 获取类别所有子类别 (无限级别-目录树) Get All Sub Categories
生成分类目录树(Category Tree)
$rootcatId = Mage::app()->getStore()->getRootCategoryId(); $categories = Mage::getModel('catalog/category')->getCategories($rootcatId); function get_categories($categories) { $array = '<ul>'; foreach ($categories as $category) { $cat = Mage::getModel('catalog/category')->load($category->getId()); $count = $cat->getProductCount(); $array .= '<li>' . '<a href="' . Mage::getModel('catalog/category')->load($category->getId())->getUrl(). '">' . $category->getName() . "(".$count.")</a>"; if ($category->hasChildren()) { $children = Mage::getModel('catalog/category') ->getCategories($category->getId()); $array .= get_categories($children); } $array .= '</li>'; } return $array . '</ul>'; } echo get_categories($categories);
我个人的用法:
function get_categories($categories,$level=0) { $array = "\n".'<ul class="'.(!$level ? 'nav navbar-nav' :'dropdown-menu dropdown-menu-level-'.$level).'">'; foreach ($categories as $category) { // $cat = Mage::getModel('catalog/category')->load($category->getId()); // $count = $cat->getProductCount(); // $array .= "\n".'<li'.($category->hasChildren()?' class="dropdown-submenu"':'').'>'. '<a href="' . Mage::getUrl($category->getUrlPath()). '" class="dropdown-toggle" data-toggle="dropdown">'. $category->getName(). "(".$count.")</a>"; $array .= "\n".'<li'.($category->hasChildren()?' class="dropdown-submenu dropdown-submenu-level-'.$level.'"':'').'>'; $array .= ' <a href="' . Mage::getModel('catalog/category')->load($category->getId())->getUrl(). '" class="dropdown-toggle" data-toggle="dropdown">'; $array .= $category->getName().($category->hasChildren()?'<span class="glyphicon glyphicon-play glyphicon-level-'.$category->getLevel().'" aria-hidden="true"></span>':''); $array .= ' </a>'; if ($category->hasChildren()) { $children = Mage::getModel('catalog/category')->getCategories($category->getId()); $array .= get_categories($children,$category->getLevel()); } $array .= '</li>'."\n"; } return $array . '</ul>'."\n"; }
打印所有类别的方法如下:
//获取所有激活状态的分类模型集合 $categories = Mage::getModel( 'catalog/category' )->getCollection() ->addAttributeToSelect( '*' ) // then the magic happens here: //->addAttributeToFilter('level', array('eq'=>2)) ->addIsActiveFilter(); //循环Collection,并填充数组,可以根据需要的数据进行填充,本例以分类ID作为key,对应的值作为value $allCategory = array (); foreach ( $categories as $category ) { $allCategory [ $category->getParentId()][$category->getId()] = $category ->getName().'('.$category->getLevel().')'; } echo '==><pre>'; print_r($allCategory); echo '</pre>';
获取当前类别的子类别 get subcategories of current / parent category in magento
Method 1:
if you want to get subcategories of current category:
$_currentCategoryId = Mage::registry('current_category')->getId(); $childcategories= Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect("*")->addFieldToFilter('parent_id', $_currentCategoryId)->addAttributeToSort('name', 'ASC'); foreach($childCategories as $childCategory) echo $childCategory->getName(); endforeach;
Method 2:
if you want to get subcategories of specific category:
$parentCategoryId = 3; $children = Mage::getModel('catalog/category')->getCategories($parentCategoryId); foreach ($children as $category) { echo $category->getName(); }
本文: Magento: 获取类别所有子类别 (无限级别-目录树) Get All Sub Categories