Abhi
Abhi

Reputation: 127

Get Parent Category Id in Sub Category Page - Magento

I need a small line of code. I have a category like

In Sub-Cats page i need to get Root category's Id. Like In "Sub-Cat-1" i need to get "Cat-1's" ID. You can say in children category page, i need parent category's Id. act i am using short url like "abc.com/Sub-Cat-1", nither index.php nor root category in URL. I am using Magento-1.4.1. Please help me. Thanks in advance.

Upvotes: 4

Views: 25399

Answers (4)

Abhi
Abhi

Reputation: 127

I got the solution.

echo $cat_idd = $this->getCurrentCategory()->getParentCategory()->getId(); 

Upvotes: 5

Jarnail S
Jarnail S

Reputation: 401

//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 

Hope it will help someone.

Upvotes: 1

Jarnail S
Jarnail S

Reputation: 401

// to get the category ID by product id

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

Upvotes: 2

MagePsycho
MagePsycho

Reputation: 2004

Try to use as:

echo $subCategory->getParentId();

Upvotes: 4

Related Questions