获取特定的类别级别

时间:2013-03-31 16:11:19

标签: 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 ?>

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

3 个答案:

答案 0 :(得分:9)

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

e.g。假设品牌类别等级为3

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

答案 1 :(得分: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>

答案 2 :(得分:0)

<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/

foreach($categories as $category){

    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
    if ($cat->getLevel() == 2) {
        $catName = $cat->getName().","; ?>
        <div class="brand_bg">
            <label><?php /* @escapeNotVerified */ echo __('Category :') ?></label>

            <?php echo $catName; ?>
        </div>
    <?php } ?>
<?php } ?>

?>