想要magento中的2级类别名称

时间:2014-07-01 12:00:21

标签: php magento categories catalog

 $product = Mage::getModel('catalog/product')->load($item->getProductId());
 $pro = $product->getCategoryName(); //category id is fetched here
 $category = Mage::getModel('catalog/category')->load($pro);
  • 根目录
    • 家具
    • 电子
      • 计算机
        • 处理器
    • 服装

因此,当我将处理器作为类别名称时,我想显示其电子级别2的类别名称

4 个答案:

答案 0 :(得分:1)

得到了解决方案,下面的代码将获取当前类别,然后继续获取父类别,直到它获得最高类别(但不是根类别)

$product = Mage::getModel('catalog/product')->load($item->getProductId());
                    $pro = $product->getCategoryName(); //category id is fetched here

                    $category = Mage::getModel('catalog/category')->load($pro);

                    if ($category)
                    {
                        while($category->getLevel() != 2)
                        {
                            $category = $category->getParentCategory();
                            if (!$category)
                            {
                                break;
                            }
                        }
                        if ($category)
                        {
                            echo $category->getName();
                        }
                        else
                        {
                            echo 'Cannot find parent category';
                        }
                    }

答案 1 :(得分:0)

$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);

答案 2 :(得分:0)

您可以使用此

$categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addIsActiveFilter()
                ->addAttributeToFilter('level',2)

OR

<?php

$_category = Mage::registry('current_category');

$_category = $this->getCurrentCategory();

// Category Name

echo $_category->getName();

// Category Level

echo $_category->getLevel();

?>

答案 3 :(得分:0)

哟可以使用这样的东西

public function get_cat_from_product($prod_id,$level){

    $categoryIds = Mage::getModel('catalog/product')->load($prod_id)->getCategoryIds();

    if(count($categoryIds) ){
        $firstCategoryId = $categoryIds[$level-1]; //level 2 -> pos 1
        $_category = Mage::getModel('catalog/category')->load($firstCategoryId);

        echo $_category->getName();
    }else{
        return null;
    }
}