2012-08-23 66 views
0

我將以下代碼放入magento基本模板文件view.phtml獲得magento中的父類別id

$ categories = $ _product-> getCategoryIds(); 的print_r($類別)

與樣本數據,當Ottoman產品頁面,其中麪包屑是

Home/Furniture/Living Room/Ottoman 

上的輸出Array ([0] => 22) 22是Living Room ID。如何獲得Furniture and Living Room ID。謝謝

回答

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 
} 
+0

代碼輸出傢俱的id不是客廳。有沒有辦法輸出他們兩個。 – down

+0

是否有辦法將它們放入一個變量中。謝謝 – down

-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()); 
} 
+0

但是這隻搜索根類別的孩子而不是類別的父母。 – balrok