Sven
Sven

Reputation: 6338

How to determine which top level category is active

On my catalog page I want to get the currently active top level category.

Example:

1. Top Level Category
  1.1 Sub Category
    1.1.1 Sub sub Category
    1.1.2 Sub sub Category
  1.2 Sub Category
2. Top Level Category
  2.1 Sub Category
  2.2 Sub Category

Let's say I am in sub sub category 1.1.1. How do I get to know that '1. Top Level Category' is the current top level category?

I tried the following but it didn't work for me:

            <?php $_cat_helper = Mage::helper('catalog/category') ?>
            <?php $_categories = $_cat_helper->getStoreCategories() ?>

            <?php foreach ($_categories as $_category): ?>
                <?php if ($this->isCategoryActive($_category)): ?>
                <?php echo $_category->getName() ?> <br>
                <?php endif; ?>
            <?php endforeach; ?>

Please help :-)


Update

If I put $this->isCategoryActive($_category) into catalog/product/list.phtml I get the following error:

Invalid method Mage_Catalog_Block_Product_List::isCategoryActive(Array
(
    [0] => 
)
)


Trace:
#0 .../default/template/catalog/product/list.phtml(53): Varien_Object->__call('isCategoryActiv...', Array)
#1 .../default/template/catalog/product/list.phtml(53): Mage_Catalog_Block_Product_List->isCategoryActive(NULL)

Upvotes: 1

Views: 1448

Answers (2)

Sven
Sven

Reputation: 6338

Figured it out:

<?php $_categories = $this->getStoreCategories();?>

<?php foreach($_categories as $_category): ?>
      <?php if($this->isCategoryActive($_category)): ?>
            <?php echo $_category->getName(); ?>
      <?php endif; ?>
<?php endforeach; ?>

Make sure the block type is catalog/navigation

Upvotes: 1

giaour
giaour

Reputation: 4061

Get the current category and climb up the category tree. You want the level 2 category (level 1 is the catalog root). Try something like this:

$category = Mage::getModel('catalog/category')
                    ->load(Mage::registry('current_category'));

while ($category->getLevel() != 2) {
    $category = Mage::getModel('catalog/category')
                        ->load($category->getParentId());
}

Upvotes: 1

Related Questions