7

如何从 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->getName(); 我只想显示品牌名称,可以吗?

4

3 回答 3

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 ?>
于 2013-04-01T05:42:14.550 回答
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>
于 2014-01-27T09:26:28.857 回答
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 } ?>

?>
于 2018-11-30T10:23:22.213 回答