2012-07-29 92 views
4

我需要一小段代碼。我喜歡獲取子類別頁中的父類別ID - Magento

  • CAT-1
    • 子CAT-1
    • 子的Cat-2
  • 的Cat-2
    • 子-CAT-3
    • 一個類別
    • 分類等級4

在Sub-Cats頁面中,我需要獲取Root類別的Id。就像在「Sub-Cat-1」中一樣,我需要得到「Cat-1」的ID。你可以說在兒童類別頁面中,我需要父類別的ID。我使用的是像「abc.com/Sub-Cat-1」這樣的短url,無論是index.php還是URL中的根類別。 我正在使用Magento-1.4.1。請幫幫我。提前致謝。

回答

2

嘗試使用爲:

echo $subCategory->getParentId(); 
+0

Thankx MagePsycho,但它不工作... – Abhi 2012-07-29 17:42:10

+0

你能告訴我你在哪裏使用什麼代碼? – MagePsycho 2012-07-29 18:00:39

5

我得到了解決。

echo $cat_idd = $this->getCurrentCategory()->getParentCategory()->getId(); 
1
//Display all categories. 

function get_categories(){ 

    $category = Mage::getModel('catalog/category'); 
    $tree = $category->getTreeModel(); 
    $tree->load(); 
    $ids = $tree->getCollection()->getAllIds(); 
    $arr = array(); 
     if ($ids){ 
      foreach ($ids as $id){ 
       $cat = Mage::getModel('catalog/category'); 
       $cat->load($id); 
       $arr[$id] = $cat->getName(); 
     } 
} 

return $arr; 

} 


$arr = get_categories(); 
$arr = array_flip($arr); 




//print the array 

希望它會幫助別人。

2
// to get the category ID by product id 

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