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

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

问题描述

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

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

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

解决方案

经过一些实验后,我已经开始工作了!下面是在

中使用的新代码

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

function _renderCategoryMenuItemHtml(如果本地化,将本地"替换为核心")

protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false, $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false){如果 (!$category->getIsActive()) {返回 '​​';}$html = 数组();//获取所有孩子if (Mage::helper('catalog/category_flat')->isEnabled()) {$children = (array)$category->getChildrenNodes();$childrenCount = count($children);} 别的 {$children = $category->getChildren();$childrenCount = $children->count();}$hasChildren = ($children && $childrenCount);//获取产品列表$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 = '';如果 ($hasProduct1 >= 1) {$l = $level+1;foreach ($_productCollection 作为 $_product) {$cur_product = Mage::getModel('catalog/product')->load($_product->getId());如果 ($cur_product->getStatus()) {$phtmlChildren .= '<li';$phtmlChildren .= 'class="level'.$l;$phtmlChildren .= 'nav-'.$this->_getItemPosition($l);如果($k == $hasProduct1){$phtmlChildren .= 'last';}$phtmlChildren .= '">'."
";$phtmlChildren .= ' <a href="'.$cur_product->getProductUrl().'">'.$this->htmlEscape($cur_product->getName()).'</a>'."
";$phtmlChildren .= '</li>';$k++;}}}//选择活跃的孩子$activeChildren = array();foreach ($children 作为 $child) {如果 ($child->getIsActive()) {$activeChildren[] = $child;}}$activeChildrenCount = count($activeChildren);$hasActiveChildren = ($activeChildrenCount > 0);//准备列表项 html 类$classes = array();$classes[] = 'level' .$级别;$classes[] = 'nav-' .$this->_getItemPosition($level);如果 ($this->isCategoryActive($category)) {$classes[] = '活动';}$linkClass = '';如果 ($isOutermost && $outermostItemClass) {$classes[] = $outermostItemClass;$linkClass = 'class="'.$outermostItemClass.'"';}如果 ($isFirst) {$classes[] = '第一';}如果($isLast){$classes[] = 'last';}如果($hasActiveChildren){$classes[] = '父母';}//准备列表项属性$attributes = array();如果(计数($类)> 0){$attributes['class'] = implode(' ', $classes);}如果 ($hasActiveChildren && !$noEventAttributes) {$attributes['onmouseover'] = 'toggleMenu(this,1)';$attributes['onmouseout'] = 'toggleMenu(this,0)';}//组合带有属性的列表项$htmlLi = '<li';foreach ($attributes as $attrName => $attrValue) {$htmlLi .= ' ' .$属性名.'="' .str_replace('"', '"', $attrValue) .'"';}$htmlLi .= '>';$html[] = $htmlLi;$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';$html[] = ''.$this->escapeHtml($category->getName()) .'</span>';$html[] = '</a>';//渲染产品"子项$htmlChildren = '';如果($hasChildren){$j = 0;foreach ($children 作为 $child) {如果 ($child->getIsActive()) {$htmlChildren .= $this->_renderCategoryMenuItemHtml($child, $level + 1, $j++ >= $k);}}}if ((!empty($htmlChildren)) || (!empty($phtmlChildren))) {$html[] = '<ul class="level'.$level.'">'."
".$htmlChildren.$phtmlChildren.'</ul>';}//渲染孩子$htmlChildren = '';$j = 0;foreach ($activeChildren 作为 $child) {$htmlChildren .= $this->_renderCategoryMenuItemHtml($孩子,($level + 1),($j == $activeChildrenCount - 1),($j == 0),错误的,$outermostItemClass,$childrenWrapClass,$noEventAttributes);$j++;}如果 (!empty($htmlChildren)) {如果($childrenWrapClass){$html[] = '<div class="' . $childrenWrapClass . '">';}$html[] = '<ul class="level' . $level . '">';$html[] = $htmlChildren;$html[] = '';如果($childrenWrapClass){$html[] = '

';}}$html[] = '';$html = implode(" ", $html);返回 $html;}

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

在 v.1.6.1 上测试

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

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?

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 (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 .= '">'."
";
                $phtmlChildren .= ' <a href="'.$cur_product->getProductUrl().'">'.$this->htmlEscape($cur_product->getName()).'</a>'."
";
                $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.'">'."
".$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("
", $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

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

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