2012-07-29 89 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();