2

I was just trying to find a code that can get current category and all the parent categories, also if there is a way to get the level of the categories to have a control over them. I have a code but it gets only the parent of the current category:

<?php
    $currentCat = Mage::registry('current_category');
    if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
    {
        // current category is a toplevel category
        $loadCategory = $currentCat;
    } else {
        // current category is a sub-(or subsub-, etc...)category of a toplevel category
        // load the parent category of the current category
        $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
    }
    $subCategories = explode(',', $loadCategory->getChildren());
    foreach ( $subCategories as $subCategoryId )    
    {
        $cat = Mage::getModel('catalog/category')->load($subCategoryId);
        if($cat->getIsActive())
        {
            echo '<li>';
            echo '<a style="color:green" href="'.$cat->getURL().'">';            
            echo $cat->getName();
            echo '</a>';
            echo '</li>';
        }
    }

?>

Thanks.

7

let's say that $category is your current category.
You can call $category->getParentCategories() and you will get an instance of Mage_Catalog_Model_Resource_Category_Collection.
You can loop through it and get each category in the tree branch.
You should be able to call ->getLevel() on each of these categories to see how high or low they are in the tree.

[EDIT]
You can do this to loop through all the parent categories

foreach ($category->getParentCategories() as $parent) {
    //do something with $parent.
    if ($parent->getLevel() == 2) {
        //then $parent is a top level category
    }
}
  • Hi can you please give a bit more info I am all dazed here. – mysticalghoul Sep 2 '15 at 5:57
  • See my edit. I'm not sure what more you need. – Marius Sep 2 '15 at 6:00
  • Yes that is exactly what i need the thing is i need to keep the level printed even if its the next one. Like this page basically i need a drop down search like this: allbatteries.co.uk/battery/car-battery.html – mysticalghoul Sep 2 '15 at 6:09
  • Ok ok sounds like i asked way too much. Just tell me how to make this work for the moment its not getting anything: if ($parent->getLevel() == 3) { echo $category->getName(); } – mysticalghoul Sep 2 '15 at 6:30
2

Given current category:

$currentCat = Mage::registry('current_category');

You can get all parent ids by the path, which is in the form 1/2/3/4, where in this example 1 is the system root category (this is actually always 1), 4 the current category 3 the immediate parent and 2 the second parent (the root category of the store)

$parentIds = explode('/', $currentCat->getPath());
array_pop($parentIds);
array_shift($parentIds);

Now $parentIds is an array with [2,3].

  • If you want to load the categories and not include the store root category, please refer to @Marius answer instead. – Fabian Schmengler Sep 1 '15 at 13:04
  • Your's and Marius's answers are great but i need to get category names urls too. – mysticalghoul Sep 1 '15 at 13:08
  • When you loop (foreach) over $category->getParentCategories(), you should be able to use getName() and getUrl() on them – Fabian Schmengler Sep 1 '15 at 13:10
  • Alright the Array ( [0] => 2 [1] => 3 [2] => 7 ), printing nicely but (foreach) crashing the page. – mysticalghoul Sep 1 '15 at 13:18
  • I was refering to Marius' answer ;) Mine only gives you the ids, which you could use to filter a category collection but using getParentCategories() is more straightforward and you should do it – Fabian Schmengler Sep 1 '15 at 13:20
0

Replace your first line:

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();

With

$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$category = checkCategoryLevel($category);
function checkCategoryLevel($category) {
    if( $category->getLevel() == 2) {
        foreach ($category->getParentCategories() as $parent) {
            if( $category->getId() != $parent->getId()) {
                return Mage::getModel('catalog/category')->load($parent->getId());
            }
        }
    }
}

If it works, place "checkCategoryLevel($category)" somewhere in block file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.