Magento:如何在主导航菜单的下拉列表中添加活动产品 [英] Magento: HOW-TO add active products in a drop-down in Main Navigation Menu

查看:117
本文介绍了Magento:如何在主导航菜单的下拉列表中添加活动产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我试图改变 core / Mage / Catalog / Block / Navigation.php 并尝试假冒产品作为类别,但对象要求是非常具体的。由于创建菜单的功能是递归的,所以它只适用于实际的类别,没有其他的。任何想法?

I've attempted to alter core/Mage/Catalog/Block/Navigation.php and try to 'fake' products as categories but the object requirement is very specific. Since the function to create the menu is recursive it will only work on actual categories and nothing else. Any ideas?

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

I know another option is to create 2nd level categories and name them as my products and then do a rewrite in .htaccess but this is not dynamic and very messy.

推荐答案

经过一番实验,我已经有了这个工作!以下是

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 (如果本地化,则将本地替换为core)

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

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

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

在v.1.6.1上测试

这篇关于Magento:如何在主导航菜单的下拉列表中添加活动产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆