Magento Forum

   
Getting Parent Category ID: $_category->parent_id returning 0 for all subcategories
 
HAL10001
Member
 
Avatar
Total Posts:  52
Joined:  2008-06-02
Ann Arbor, MI
 

<?php foreach ($this->getStoreCategories() as $_category): ?>

echo $_category->parent_id;

<?php endforeach ?>

This always returns 0, even though some of my store categories have parents. Why might this be, and is there a better way to get a categories parent ID?

 
Magento Community Magento Community
Magento Community
Magento Community
 
HAL10001
Member
 
Avatar
Total Posts:  52
Joined:  2008-06-02
Ann Arbor, MI
 

I’m going to assume that this is simply not possible, and that it is a bug. Even within Magento’s code I see no use of parent_id returning the proper parent_id. However, I did program a solution where I can still grab a parent’s ID, and simply pass it down to the children with a variable. This appears to be the best possible solution:

<ul>
    
<?php
    
        
foreach ( $this->getStoreCategories() as $_category {
            
if ( $this->isCategoryActive$_category ) ) {

    ?>
    
    
<li><h2><?php echo $_category->getName() ?></h2></li>
    
    
<?php
            
                $children 
$_category->getChildren();
                
$hasChildren $children && $children->count();
                if ( 
$hasChildren {
                    $this_id 
$_category->getId();
                    foreach ( 
$children as $child {
                        
if ( $child->getIsActive() ) {

    ?>
    
    
<li<?php echo ( $this->isCategoryActive$child ) ) ? " class='current'" "" ?>><a href="<?php echo $this->getCategoryUrl$child ); ?>"><?php echo $child->getName(); ?></a></li>
    
    
<?php
    
                        }
                    }
                }
            }
        }
    
    ?>
</ul>

I am not actually using $this_id in the example, but it is there if I wanted to use it, and when I loop through a category’s children they have access to it. In case you are wondering this is also a solution to provide a left-hand navigation of sub-categories, while keeping the parent category heading. This does not recursively build out sub-sub-categories like Magento’s header navigation, assumes there is only one level of sub-categories, and uses similar code as Magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kevin Filteau
Jr. Member
 
Total Posts:  10
Joined:  2008-10-07
 

There is my garage technique to extract parent id from a category. I put this function directly in the template file I needed it.

function kevin_getParentIdFromCategoryPath($_category{
    
// Get path
        
$path kevin_getCategoryAttribute($_category6);
        
$parsed_path explode("/",$path);
        
$pos count($parsed_path) - 2;
        return(
$parsed_path[$pos]);
}

This function retreive category attributes from the string. Retrive elements
before category description, if I have comma in it, it’s not so easy to split
elements.

if( !function_exists('kevin_getCategoryAttribute') ) {
        
function kevin_getCategoryAttribute($_category$attr_id{
            
        
// 1. Split the string
        /*  
            Array
            (
                [0] => 13
                [1] => 3
                [2] => 3
                [3] => 12                                parent_id
                [4] => 2008-10-06 21:27:04                created_at
                [5] => 2008-10-24 01:08:14
                [6] => 1/2/12/13                        path
                [7] => 7
                [8] => 3
                [9] => 0
                [10] => 1/2/12/13
                [11] => 1
                [12] => artists/kevin-filteau
                [13] => Kevin Filteau
                [14] => kevin-filteau
            )
        
        */
            
            
$parsed explode(',',$_category);
            return(
$parsed[$attr_id]);
        
}
    }

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kevin Filteau
Jr. Member
 
Total Posts:  10
Joined:  2008-10-07
 

I found a better way to retrieve category data. In reality, it’s an object and we can get object properties by using the get_object_vars function. This also solve my comma splitting problems. So try a var_dump and you will see what category info you can get for this object.

+ This method only retrieve data stored for All Store Views.

 
Magento Community Magento Community
Magento Community
Magento Community
 
miked2004
Sr. Member
 
Avatar
Total Posts:  214
Joined:  2007-12-13
Atlanta, Georgia
 

I use this to get the top category “not just the parent category”
You need to pass in a category object;
I have level set to 2 because I did not want to get the root catalog as a category. just items directly under the root catalog

public function getParentTopCategory($category)
    
{
        
if($category->getLevel() == 2){
            
return $category;
        
else {
            $parentCategory 
Mage::getModel('catalog/category')->load($category->getParentId());
            return 
$this->getParentTopCategory($parentCategory);
        
}
    }

 
Magento Community Magento Community
Magento Community
Magento Community
 
lizlux
Member
 
Avatar
Total Posts:  40
Joined:  2009-08-03
 
HAL10001 - 05 September 2008 04:47 AM

I’m going to assume that this is simply not possible, and that it is a bug. Even within Magento’s code I see no use of parent_id returning the proper parent_id. However, I did program a solution where I can still grab a parent’s ID, and simply pass it down to the children with a variable. This appears to be the best possible solution:

<ul>
    
<?php
    
        
foreach ( $this->getStoreCategories() as $_category {
            
if ( $this->isCategoryActive$_category ) ) {

    ?>
    
    
<li><h2><?php echo $_category->getName() ?></h2></li>
    
    
<?php
            
                $children 
$_category->getChildren();
                
$hasChildren $children && $children->count();
                if ( 
$hasChildren {
                    $this_id 
$_category->getId();
                    foreach ( 
$children as $child {
                        
if ( $child->getIsActive() ) {

    ?>
    
    
<li<?php echo ( $this->isCategoryActive$child ) ) ? " class='current'" "" ?>><a href="<?php echo $this->getCategoryUrl$child ); ?>"><?php echo $child->getName(); ?></a></li>
    
    
<?php
    
                        }
                    }
                }
            }
        }
    
    ?>
</ul>

I am not actually using $this_id in the example, but it is there if I wanted to use it, and when I loop through a category’s children they have access to it. In case you are wondering this is also a solution to provide a left-hand navigation of sub-categories, while keeping the parent category heading. This does not recursively build out sub-sub-categories like Magento’s header navigation, assumes there is only one level of sub-categories, and uses similar code as Magento.

Hey HAL10001,
This looks like it should do what I need to do, which is display a list of subcategories of one of my main categories the home page, but when I use this code nothing shows up.  I’ve pasted it into a new file called /app/design/frontend/default/default/template/catalog/category/list.phtml and called it from the home page under manage CMS pages.  Any ideas as to why nothing is appearing?
Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Ella S.
Jr. Member
 
Total Posts:  21
Joined:  2009-06-29
 

Thanks Kevin.

Quick solution to retrieve the parent id:

$ella_current_category Mage::registry('current_category');
$ella_parent_id $ella_current_category->parent_id;

 
Magento Community Magento Community
Magento Community
Magento Community
 
chapagain
Sr. Member
 
Avatar
Total Posts:  202
Joined:  2009-07-19
 

Have a look at this : http://blog.chapagain.com.np/index.php/magento-get-parent-id-of-simple-product-associated-to-configurable-product/

 
Magento Community Magento Community
Magento Community
Magento Community
 
sekret
Jr. Member
 
Total Posts:  8
Joined:  2010-08-09
 
HAL10001 - 05 September 2008 04:47 AM

I’m going to assume that this is simply not possible, and that it is a bug. Even within Magento’s code I see no use of parent_id returning the proper parent_id. However, I did program a solution where I can still grab a parent’s ID, and simply pass it down to the children with a variable. This appears to be the best possible solution:

<ul>
    
<?php
    
        
foreach ( $this->getStoreCategories() as $_category {
            
if ( $this->isCategoryActive$_category ) ) {

    ?>
    
    
<li><h2><?php echo $_category->getName() ?></h2></li>
    
    
<?php
            
                $children 
$_category->getChildren();
                
$hasChildren $children && $children->count();
                if ( 
$hasChildren {
                    $this_id 
$_category->getId();
                    foreach ( 
$children as $child {
                        
if ( $child->getIsActive() ) {

    ?>
    
    
<li<?php echo ( $this->isCategoryActive$child ) ) ? " class='current'" "" ?>><a href="<?php echo $this->getCategoryUrl$child ); ?>"><?php echo $child->getName(); ?></a></li>
    
    
<?php
    
                        }
                    }
                }
            }
        }
    
    ?>
</ul>

I am not actually using $this_id in the example, but it is there if I wanted to use it, and when I loop through a category’s children they have access to it. In case you are wondering this is also a solution to provide a left-hand navigation of sub-categories, while keeping the parent category heading. This does not recursively build out sub-sub-categories like Magento’s header navigation, assumes there is only one level of sub-categories, and uses similar code as Magento.

Hey!Thanks for the solution!It is nicely done!

Btw , how i get the count subcategories for like this :

Categories
- subcategories ( <how i get this ?> )
- subcategories ( < how i get this?> )

Thanks again

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anjan Barik
Jr. Member
 
Avatar
Total Posts:  18
Joined:  2010-03-19
Kolkata
 

By getParentId() method you can get parentid of a subcategory .For the code visit http://xhtmlandcsshelp.blogspot.com/2010/08/how-to-get-parent-category-id-in.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
satish
Enthusiast
 
Total Posts:  812
Joined:  2008-03-24
34,mantri Sadan,Shivaji Nagar,Nagpur,MS,India 4400
 

You will get parent categories of current category .
<?php
$storeId = Mage::app()->getStore()->getId();
$_category = Mage::getModel('catalog/category');
$product = Mage::getModel('catalog/product');
$current_category = Mage::registry('current_category');
$categoryParent = explode(',', $current_category->getParentCategory());
print_r($categoryParent);
?>

Satish

 
Magento Community Magento Community
Magento Community
Magento Community
 
GhMaster
Jr. Member
 
Avatar
Total Posts:  24
Joined:  2010-05-31
Yerevan, Armenia
 

Hi,
I found that this topic is very similar to my problem. I have an online catalog with a lot of categories, subcategories, attributes, products, etc.
And I need to customize my filtering. I need the filtering to be based on current category. (like sorting). Updating code with “if” or “switch” operators will not solve my problem, because my catalog is in process of changings and I need dinamic solution.

There are 2 general problems:

1. if I have an electronics category and many subcategories in it, and I don’t want all electronics products attributes to be shown in that category, but in same time I want to display several of them (for example Brand).

2. I have attributes, which must be shown in filtering of one category and don’t shown in other one.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Magebooker
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2011-09-17
United States
 

get parent category id

<?php $parentId $_category->getParentId() ?>

 
Magento Community Magento Community
Magento Community
Magento Community
 
eStonia_Agency
Jr. Member
 
Avatar
Total Posts:  26
Joined:  2007-10-24
London
 
Magebooker - 13 December 2011 02:49 PM

get parent category id

<?php $parentId $_category->getParentId() ?>

For me with 1.7.0.2 it gives the ultimate parent not one above.

I have:

root category (2)
-subcat (7)
-sub-subcat
-sub-subcat

in sub-subcats want to get result as 7 but I’m getting 2.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top