简体   繁体   English

想要在magento中的2级类别名称

[英]want level 2 category name in magento

 $product = Mage::getModel('catalog/product')->load($item->getProductId());
 $pro = $product->getCategoryName(); //category id is fetched here
 $category = Mage::getModel('catalog/category')->load($pro);
  • Root Catalog 根目录
    • Furniture 家具
    • Electronics 电子产品
      • Computer 电脑
        • Processor 处理器
    • Apparel 服饰

So when i get processor as category name, i want to display its level 2 category name that is Electronics 因此,当我获得处理器作为类别名称时,我想显示其级别2的类别名称,即电子

got the solution, The code below will get the current category and then keep getting the parent category until it gets the highest category (but not the root category) 得到了解决方案,下面的代码将获取当前类别,然后继续获取父类别,直到获得最高类别(而不是根类别)为止。

$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';
                        }
                    }
$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);

You can use this 你可以用这个

$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();

?>

Yo can use something like this 哟可以用这样的东西

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM