8/26/2010

How to get parent category Id in Magento

To get Parent Category Id you must know the current category Id, for that you need to write
<?php
$_cat = new Mage_Catalog_Block_Navigation();
$curent_cat = $_cat->getCurrentCategory();
$curent_cat_id = $curent_cat->getId();?>

Now you can get Parent Category Id.
write the code given below to get the Id
<?php $parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();
echo $parentId; // $parentId will print your current category's parent Id
?>

2 comments:

  1. very thanks man.
    i need parent category name , i did this function.
    function findparent($idnow){
    $ID2='';
    $ID1='';
    $ID='';
    $parentId = Mage::getModel('catalog/category')->load($idnow)->getParentId();
    if($parentId==''||$parentId=='1'||$parentId=='3'){
    //Root category id = '3';
    $ID1 = $idnow;
    }else{$ID1 = $parentId; }
    $parentId2 = Mage::getModel('catalog/category')->load($ID1)->getParentId();
    if($parentId2==''||$parentId2=='1'||$parentId2=='3'){$ID2 = $ID1; }else{$ID2=$parentId2;}
    $parentId3 = Mage::getModel('catalog/category')->load($ID2)->getParentId();
    if($parentId3==''||$parentId3=='1'||$parentId3=='3'){$ID = $ID2; }else{$ID=$parentId3;}
    $ok = Mage::getModel('catalog/category')->load($ID);
    $kq = strtoupper(trim($ok->getName()));
    return $kq;

    }

    //AND

    $_cat = new Mage_Catalog_Block_Navigation();
    $curent_cat = $_cat->getCurrentCategory();
    $curent_cat_id = $curent_cat->getId();
    $t= findparent($curent_cat_id);
    echo $t;//parent category name.
    this code can using on both category page and product details page.
    Thanks again and goodluck !.
    See live site :
    http://www.pet-pillow.de/shop/index.php/sonstiges/hoodies.html

    ReplyDelete
  2. Very much Thanks!!!!!!!

    ReplyDelete