Magento: HOW-TO add active products in a drop-down in Main Navigation Menu

后端 未结 3 1949
有刺的猬
有刺的猬 2021-01-14 20:44

Instead of categories I would like products to appear in the drop-down navigation menu similar to lowes.com. Is this possible? I am not paying for anything either :)

3条回答
  •  囚心锁ツ
    2021-01-14 21:11

    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 .= '_getItemPosition($l);
                    if ($k == $hasProduct1) {
                        $phtmlChildren .= ' last';
                    }
                    $phtmlChildren .= '">'."\n";
                    $phtmlChildren .= ' '.$this->htmlEscape($cur_product->getName()).''."\n";
                    $phtmlChildren .= '
  • '; $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 = ' $attrValue) { $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"'; } $htmlLi .= '>'; $html[] = $htmlLi; $html[] = ''; $html[] = '' . $this->escapeHtml($category->getName()) . ''; $html[] = ''; // 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[] = '
      '."\n".$htmlChildren.$phtmlChildren.'
    '; } // 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; }

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

    Tested on v.1.6.1

提交回复
热议问题