Magento top navigation show products in category

I had a structure like this in admin

– Category
– – SubCategory1
– – – Product 1
– – – Product 2
– – – Product 3
– – SubCategory2
– – – Product 1
– – – Product 2
– – – Product 3

but the default magento navigation would only list my categories and subcategories, not the products. after a bit of tweaking around i noticed that both SubCategory1 and SubCategory2 are level 1 so i only needed to find a way to display products for those categories. That is done easily by extending /core/Mage/Catalog/Block/Navigation.php into your local (/local/Mage/Catalog/Block/Navigation.php) and replacing the function _renderCategoryMenuItemHtml with the one below:

    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
        $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
    {
        if (!$category->getIsActive()) {
            return '';
        }
        $html = array();

        // get all children
        if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        }
        $hasChildren = ($children && $childrenCount);

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);

        // prepare list item html classes
        $classes = array();
        $classes[] = 'level' . $level;
        $classes[] = 'nav-' . $this->_getItemPosition($level);
        if ($this->isCategoryActive($category)) {
            $classes[] = 'active';
        }
        $linkClass = '';
        if ($isOutermost && $outermostItemClass) {
            $classes[] = $outermostItemClass;
            $linkClass = ' class="'.$outermostItemClass.'"';
        }
        if ($isFirst) {
            $classes[] = 'first';
        }
        if ($isLast) {
            $classes[] = 'last';
        }
        if ($hasActiveChildren) {
            $classes[] = 'parent';
        }

        // prepare list item attributes
        $attributes = array();
        if (count($classes) > 0) {
            $attributes['class'] = implode(' ', $classes);
        }
        if ($hasActiveChildren && !$noEventAttributes) {
             $attributes['onmouseover'] = 'toggleMenu(this,1)';
             $attributes['onmouseout'] = 'toggleMenu(this,0)';
        }

        // assemble list item with attributes
        $htmlLi = ' $attrValue) {
            $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
        }
        $htmlLi .= '>';
        $html[] = $htmlLi;

        $html[] = '';
        $html[] = '' . $this->escapeHtml($category->getName()) . '';
        $html[] = '';


		// Grabbing the products for the category if it's level is 1
		if ($level == 1) {
		
			$catId = $category->getId();
			$categorie = new Mage_Catalog_Model_Category();
			$categorie->load($catId); // this is category id
			$collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
			$html[] = '
    '; foreach ($collection as $pc) { $p = new Mage_Catalog_Model_Product(); $p->load($pc->getId()); $data = $p->_data; $html[] = '
  • '.$data['name'] .'
  • '; } $html[] = "
\n"; } // Done // render children $htmlChildren = ''; $j = 0; foreach ($activeChildren as $child) { $htmlChildren .= $this->_renderCategoryMenuItemHtml( $child, ($level + 1), ($j == $activeChildrenCount - 1), ($j == 0), false, $outermostItemClass, $childrenWrapClass, $noEventAttributes ); $j++; } if (!empty($htmlChildren)) { if ($childrenWrapClass) { $html[] = '
'; } $html[] = '
    '; $html[] = $htmlChildren; $html[] = '
'; if ($childrenWrapClass) { $html[] = '
'; } } $html[] = ''; $html = implode("\n", $html); return $html; }

The rest of the file stays the same. Note this works on magento > 1.4

4 thoughts on “Magento top navigation show products in category”

  1. wow, huge thanks! i was looking for a fix for this exact issue and you have it listed here perfectly running 1.5 CE! saved me hours! bookmarked!

  2. wow thank you so much! i was googling this issue for hours with no luck! excellent post and worked perfectly for my 1.5 CE setup! thanks a ton!

  3. Do you have any about displaying products under category in top navigation app/code/core/mage/page/html/Topmenu.php

Leave a Reply

Your email address will not be published. Required fields are marked *