我想显示产品所属类别的列表,顶级类别和子类别。如何仅加载属于当前产品的子类别(magento)
我想出了如何加载正确的topcategories,但是当我循环subcats(子类)时,它会加载该topcategory的所有子对象,而不是该产品所属的类别。
例子:
就像你可以看到它装入一吨的子类别,但只有与红色条纹的那些是产品处于子类别。
我怎样才能确保它只显示在他们的topcategories下的那些?
我的代码:
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
// ->addFieldToFilter('level',2)
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
$out = "<ul>";
foreach($categoryCollection as $cat){
$out .= "<li>";
$out .= "<b><a href='".$cat->getUrl()."'>".$cat->getName()."</a></b>";
$out .="<ul class='sub'>";
$children = Mage::getModel('catalog/category')
// ->addAttributeToFilter('entity_id', $cat->getCategoryIds())
->load($cat->getId())
->getChildrenCategories();
foreach($children as $child){
$out .="<li><a href='".$child->getUrl()."'>".$child->getName()."</a></li>";
}
$out .="</ul>";
$out .= "</li>";
}
$out .= "</ul>";
echo $out;