Magento:如何在主导航菜单的下拉列表中添加活动产品

Jar*_*ier 2 navigation product menu magento drop-down-menu

而不是类别我希望产品出现在类似于lowes.com的下拉导航菜单中.这可能吗?我不支付任何费用:)

我试图改变core/Mage/Catalog/Block/Navigation.php并尝试将产品"伪造"为类别,但对象要求非常具体.由于创建菜单的功能是递归的,因此它只适用于实际类别而不是其他任何类别.有任何想法吗?

我知道另一种选择是创建第二级别类别并将它们命名为我的产品,然后在.htaccess中重写,但这不是动态的,而且非常混乱.

Jar*_*ier 5

经过一些实验,我得到了这个工作!以下是要使用的新代码

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

function _renderCategoryMenuItemHtml (如果本地化,则将'local'替换为'core')

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;
}
Run Code Online (Sandbox Code Playgroud)

基本上有两个新添加的部分.第一部分构建产品集合以获取相关信息(名称,URL等).第二部分将新的无序列表附加到现有根类别列表项中.希望这有助于某人.现在你不需要为那里的扩展支付99美元:)

在v.1.6.1上测试过