Parent category identifier on the page below the category - Magento

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

  • Cat-1
    • Sub-cat-1
    • Sub-cat-2
  • Cat 2
    • Sub-cat-3
    • Sub-cat-4

On the Sub-Cats page, I need to get the Root category identifier. As in "Sub-Cat-1", I need to get the identifier "Cat-1". You can tell on the page of the category of children, I need the identifier of the parent category. I use a short URL, for example, "abc.com/Sub-Cat-1", neither index.php, nor the root category in the URL. I am using Magento-1.4.1. Please help me. Thanks in advance.

+4
source share
4 answers

I got a solution.

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

Try using as:

 echo $subCategory->getParentId(); 
+2
source
 // to get the category ID by product id $productObj = Mage::getModel('catalog/product')->load($product_id); $categoryIds = $productObj->getCategoryIds(); 
+2
source
 //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 this helps someone.

+1
source

All Articles