如何在Magento的自定义菜单下获取子类别 [英] How to get subcategories under a custom menu on Magento

查看:102
本文介绍了如何在Magento的自定义菜单下获取子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功为每个需要的特定类别创建了一个自定义下拉菜单,但其中一个需要加载子类别中的子类别,并且无法使其工作。

I have successfully created a custom drop-down-menu for each specific category I needed but one of them needs to load the subcategory within a subcategory and I can't get it to work.

没有子类别中的子类别的工作代码如下,但需要了解如何添加此代码的第3级。

The working code without the "subcategory within a subcategory" is the following but I need to find out how to add the 3rd level on this code.

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


推荐答案

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

Well, as I have suggested in https://stackoverflow.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>

这篇关于如何在Magento的自定义菜单下获取子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆