0

I am designing my first eCommerce website and I am wondering if it is possible to show products as drop-down menu in the main menu?

UPDATE: I want to list all the products included in the category which is present in the main menu, when the customers hovers over that category (like it happens with listing subcategories).

Running Magento 1.9.2.4.

1

Yes its possible.

You can create your custom code into

.../template/catalog/navigation/top.phtml

or

Enable path hints and get the file path of the navigation menu

and

<?php
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
?>
<ul id="nav">
        <?php echo $_menu ?>
  <li>
      <ul>
        <?php 
foreach ($products as $_product) { ?>
   <li><?php echo $_product->getName(); ?></li>
<?php } ?>
</ul>
</li>
</ul>
  • Thank you for your response. I have tried adding the code you provided to the navigation menu file, but it didn't list any products anywhere. – spektro37 Apr 28 '16 at 10:17
  • Please check your file. that is right file or not. because i have solve my issue using this code. – Jaydip Kansagra Apr 29 '16 at 12:21
  • Sorry for my mistake please retry with updated code. – Jaydip Kansagra Apr 29 '16 at 12:36
1

After a bit of experimenting I've got this working! Below is the new code to use in

app/code/core/Mage/Catalog/Block/Navigation.php

function _renderCategoryMenuItemHtml (swap 'local' for 'core' if localizing)

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);

    // get products listing
    $cur_category = Mage::getModel('catalog/category')->load($category->getId());
    $_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category)->setOrder('position','ASC');
    $k = 1;
    $hasProduct1 = $_productCollection->count();
    $phtmlChildren = '';
    if ($hasProduct1 >= 1) {
        $l = $level+1;
        foreach ($_productCollection as $_product) {
            $cur_product = Mage::getModel('catalog/product')->load($_product->getId());
            if ($cur_product->getStatus()) {
                $phtmlChildren .= '<li';
                $phtmlChildren .= ' class="level'.$l;
                $phtmlChildren .= ' nav-'.$this->_getItemPosition($l);
                if ($k == $hasProduct1) {
                    $phtmlChildren .= ' last';
                }
                $phtmlChildren .= '">'."\n";
                $phtmlChildren .= ' <a href="'.$cur_product->getProductUrl().'">'.$this->htmlEscape($cur_product->getName()).'</a>'."\n";
                $phtmlChildren .= '</li>';
                $k++;
            }
        }
    }

    // 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 = '<li';
    foreach ($attributes as $attrName => $attrValue) {
        $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
    }
    $htmlLi .= '>';
    $html[] = $htmlLi;

    $html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
    $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
    $html[] = '</a>';

    // render 'product' children
    $htmlChildren = '';
    if ($hasChildren) {
        $j = 0;
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $htmlChildren .= $this->_renderCategoryMenuItemHtml($child, $level + 1, $j++ >= $k);
            }
        }
    }
    if ((!empty($htmlChildren)) || (!empty($phtmlChildren))) {
        $html[] = '<ul class="level'.$level.'">'."\n".$htmlChildren.$phtmlChildren.'</ul>';
    }

    // 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[] = '<div class="' . $childrenWrapClass . '">';
        }
        $html[] = '<ul class="level' . $level . '">';
        $html[] = $htmlChildren;
        $html[] = '</ul>';
        if ($childrenWrapClass) {
            $html[] = '</div>';
        }
    }

    $html[] = '</li>';

    $html = implode("\n", $html);       
    return $html;
}

Basically there are two new added sections. The first section builds the product collection to get relevant info (name, url, etc). The second section appends the new unordered list inside the existing root category list items. Hope this helps someone. Now you don't need to pay $99 for the extension that is out there :)

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.