1

i.e.

I have the following category: main category (!= root)

Main category
|
|- sub category 1
|
|- sub category 2
|
|- sub category 3

My navigation,

  <ul>
    <?php echo $_menu ?>
  </ul>

Now renders

Main category

How can I get it to render just the sub categories, like

Subcategory 1
Subcategory 2
Subcategory 3

(The reason I put all my categories in one main is to be able to link to mysite.com/shop for all products. Main category therefore is /shop )

3

You can probably do that by overwriting the file app/core/Mage/Catalog/Block/Navigation.php

app/code/local/YourCompany/Catalog/etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_Catalog>
            <version>1.0</version>
        </YourCompany_Catalog>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                  <navigation>YourCompany_Catalog_Block_Navigation</navigation>
               </rewrite>
           </catalog>
       </blocks>       
    </global>
</config>

app/code/local/YourCompany/Catalog/Block/Navigation.php:

class YourCompany_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation {
    public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
    $activeCategories = array();
    foreach ($this->getStoreCategories() as $child) {
        if ($child->getIsActive()) {
            $activeCategories[] = $child;
        }
    }

    $activeCategoriesCount = count($activeCategories);
    $hasActiveCategoriesCount = ($activeCategoriesCount > 0);

    if (!$hasActiveCategoriesCount) {
        return '';
    }

    $html = '';
    $j = 0;
    foreach ($activeCategories as $category) {
        foreach ($category->getChildren() as $child) { // Added this loop to bypass the first top categories
            $html .= $this->_renderCategoryMenuItemHtml(
                $child,
                $level,
                ($j == $activeCategoriesCount - 1),
                ($j == 0),
                true,
                $outermostItemClass,
                $childrenWrapClass,
                true
            );
            $j++;
        }
    }

    return $html;
}
}

app/etc/modules/YourCompany.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <YourCompany_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </YourCompany_Catalog>
    </modules>
</config>

Global overview of the files to creates: app code local YourCompany Catalog Block Navigation.php etc config.xml etc modules YourCompany_Catalog.xml

  • Are there any additional steps? Nothing happens... And I also guess you mean app > code > local ? – Frank Mar 24 '15 at 16:19
  • Yes in app/code/local, my answer has been edited by someone else with this error. You also have to declare your new local module (I've edited my answer) – christophe Mar 24 '15 at 16:23
  • Hm still nothing. I've also refreshed my cache. I'm also sure I copy pasted everything in the right directory, as well as a find & replace for YourCompany edit: and I also had config.xml already capitalized – Frank Mar 24 '15 at 16:28
  • I might found what I'm doing wrong. My menu is located @ page/html/topmenu.phtml (instead of /Catalog/ as your answer suggests (I guess). I guess that changes your answer? – Frank Mar 24 '15 at 17:00
  • It is weird that nothing happened. The phtml file is not where your menu is created. It defines the custom elements of your menu but the "classic" menu creation with the categories is in the navigation.php file. My answer wasn't quite right, but I tried it today and the following works. – christophe Mar 25 '15 at 9:36

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.