且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Magento的自定义菜单下获取子类别

更新时间:2023-11-30 15:35:22

嗯,正如我在https://***.com/a/14586422/653867 ,您必须为第二级类别加载类别对象:

Well, as I have suggested in https://***.com/a/14586422/653867 you have to load a category object for your second level categories:

$cat = Mage::getModel('catalog/category')->load($category->getEntityId());

然后你可以通过执行

$children = $cat->getChildrenCategories();

$ children 变量是键入 Mage_Catalog_Model_Resource_Category_Collection ,您可以迭代通过它输出下一级别类别

The $children variable is a collection of type Mage_Catalog_Model_Resource_Category_Collection, and you can iterate through it to output the next level categories

我认为,您的代码可以如果您首先在$ main上调用了getChildrenCategories(),则改进了一点。你不必加载一个循环中的每个孩子,这可以是性能惩罚。而是使用这个(并且实际上可以通过递归调用进一步改进,但是这样的设置将包括创建额外的块,这对于这种特定情况可能太麻烦了):

I think, your code can be improved a bit if you called getChildrenCategories() on your $main in the first place. You wouldn't have to load every child in a loop, which can be performance punishing. Instead use this (and it can actually be improved even further with recursive calls, but such setup would include creating extra blocks, which might be too much hassle for this particular case):

 <?php $main = Mage::getModel('catalog/category')->load(355) ?>
 <li class="eight"><a href="<?php echo $main->getUrl() ?>"><?php echo $main->getName(); ?></a>
 <?php $children = $main->getChildrenCategories(); ?>
 <ul class="nav_static">
 <?php foreach ($children as $category): ?>
 <li>
 <a href="<?php echo $category->getUrl(); ?>">
 <?php echo $category->getName();

 $subCategories = $category->getChildrenCategories();
 foreach ($subCategories as $subCat) {
 /**
  *your code to output the next level categories
  */
 }
 ?>
 </a>
 </li>
 <?php endforeach; ?>
 </ul>
 </li>