获取magento中的父类别ID

时间:2012-08-23 05:29:58

标签: magento

我将以下代码放入magento基本模板文件view.phtml

$ categories = $ _product-> getCategoryIds(); 的print_r($类别)

使用示例数据,在Ottoman产品页面上,哪个痕迹是

 Home / Furniture / Living Room / Ottoman

输出为Array ( [0] => 22 ),22为Living Room id。如何获得Furniture and Living Room id。谢谢

2 个答案:

答案 0 :(得分:2)

$categories=$_product->getCategoryIds();
foreach ($categories as $category)
{
    $info = Mage::getModel('catalog/category')
            ->load($category)
            ->getParentCategory();
    echo $info->getId(); //display category ID
    //var_dump($info->debug());  # display model contents
}

答案 1 :(得分:-1)

以下是获取所有类别ID的方法:

//load root category 
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')
                ->load($rootCategoryId);

//load all children categories of the root category
$childrenCategories = $rootCategory->getChildrenCategories();

现在你可以像这样访问他们的ID:

foreach ($childrenCategories as $category) {
        var_dump($category->getId());
}