获取特定的类别级别

and*_*aer 7 magento

如何从Magento获得特定的类别级别,我的类别设置现在看起来像这样.

root_catalog
    |-Shop
        |-Shoes
        |-T-shirts
    |-Brands
        |-Nike
           |-Womens
           |-Mens
        |-Adidas
        |-Asics

<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
    <?php echo $category->getName(); ?>
<?php endif ?>
Run Code Online (Sandbox Code Playgroud)

调用$ category-> getName()时; 我想只显示品牌名称,这可能吗?

小智 9

您可以从中获取类别级别 $category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel() ,然后使用您的品牌名称类别级别进行检查,如果匹配则显示名称.

例如,假设品牌类别等级为3

<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
    <?php if($category->getLevel() == 3)
        echo $category->getName(); ?>
    <?php endif ?>
<?php endif ?>
Run Code Online (Sandbox Code Playgroud)


Set*_*aki 5

ANKIT的答案很好,但可以通过实际查询特定级别而不是加载整个集合并进行条件化来改进它.例如,如果您想要获得特定级别的所有类别:

<ul>
<?php $categories = Mage::getModel('catalog/category')
                         ->getCollection()
                         // magic is prepared here..
                         ->addAttributeToSelect('*')
                         // then the magic happens here:
                         ->addAttributeToFilter('level', array('eq'=>2))
                         ->load();

      foreach($categories as $category):
?>
<li>$category->getName()</li>
<?php endforeach; ?>
</ul>
Run Code Online (Sandbox Code Playgroud)