首页 文章

如何在Magento菜单中列出产品

提问于
浏览
0

喜欢这个问题:Magento: HOW-TO add active products in a drop-down in Main Navigation Menu

...我想列出主菜单中某个类别的产品 . 我试图使用提供的代码,但它仍然无法正常工作 . 我正在使用Magento ver . 1.7.0.2所以我认为这可能是个问题 .

任何帮助,将不胜感激 .

1 回答

  • 1

    我有同样的问题,几个小时后我现在有一个解决方案 . 我正在使用Magento 1.9.1,我还需要编辑Topmenu.php文件 . 可能它不是最好的方法,但它的工作原理 . 我试图评论一切,所以也许它更容易理解 .

    我更改了'_getHtml'函数以包含产品并更改'li'标签的类,如有必要:

    protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass, $correctClasses = 0)
    {
        $html = '';
    
        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;
    
        $counter = 1;
        $childrenCount = $children->count();
    
        $parentPositionClass = $menuTree->getPositionClass();
        $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';
    
        foreach ($children as $child) {
    
            $child->setLevel($childLevel);
            $child->setIsFirst($counter == 1);
            $child->setIsLast($counter == $childrenCount);
            $child->setPositionClass($itemPositionClassPrefix . $counter);
    
            $outermostClassCode = '';
            $outermostClass = $menuTree->getOutermostClass();
    
            if ($childLevel == 0 && $outermostClass) {
                $outermostClassCode = ' class="' . $outermostClass . '" ';
                $child->setClass($outermostClass);
            }
    
            // avoid 'last'-class if there are products after categories
            $renderedAttributes = $this->_getRenderedMenuItemAttributes($child);
            if($correctClasses = 1) {
                $renderedAttributes = str_replace(' last', '', $renderedAttributes);
            }
    
            // add 'category' class to category elements
            $renderedAttributes = str_replace('class="', 'class="type-category ', $renderedAttributes);
    
            $html .= '<li ' . $renderedAttributes . '>';
            $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml($child->getName()) . '</span></a>';
    
            // check if there are more categories or products inside
            $hasProducts = $this->hasProducts($child);
            if ($child->hasChildren() || $hasProducts) {
    
                if (!empty($childrenWrapClass)) {
                    $html .= '<div class="' . $childrenWrapClass . '">';
                }
    
                // build ul-wrapper
                $html .= '<ul class="level' . $childLevel . '">';
    
                // if categories and products are in this category
                if($child->hasChildren() && $hasProducts) {
                    $correctClasses = 1;
                    $html .= $this->_getHtml($child, $childrenWrapClass, $correctClasses);
                    $html .= $this->getProducts($child, $childLevel, $correctClasses);
                // if only categories are in this category
                } elseif($child->hasChildren()) {
                    $html .= $this->_getHtml($child, $childrenWrapClass);
                // if only products are in this category
                } elseif($hasProducts) {
                    $html .= $this->getProducts($child, $childLevel);
                }
                $html .= '</ul>';
    
                if (!empty($childrenWrapClass)) {
                    $html .= '</div>';
                }
            }
            $html .= '</li>';
    
            $counter++;
        }
    
        return $html;
    }
    

    另外,我编写了3个新函数来处理所有内容 .

    一个新的小功能,从当前类别获取产品集合:

    // get product collection    
    protected function getProductCollection($child) {
        // get current category
        $catId = str_replace('category-node-', '', $child->getId());
        $curCategory = Mage::getModel('catalog/category')->load($catId);
    
        // get prouct collection from current category
        return Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($curCategory)->setOrder('position','ASC');
    }
    

    检查当前类别中是否有任何产品的功能:

    // check if there are products in this category
    protected function hasProducts($child) {
        // get number of products in current (sub-)category
        $productCount = $this->getProductCollection($child)->count();
    
        if($productCount > 0) {
            return 1;
        } else {
            return 0;
        }
    }
    

    还有一个函数可以为产品列表项构建html:

    // get product html
    protected function getProducts($child, $level, $correctClasses = 0) {
        $productCollection = $this->getProductCollection($child);
    
        // set product counter
        $p = 1;
    
        // get number of products in current (sub-)category
        $productCount = $productCollection->count();
    
        $pChild = '';
        if ($productCount > 0) {
            $level++;
            foreach ($productCollection as $product) {
    
                // get current product in loop
                $curProduct = Mage::getModel('catalog/product')->load($product->getId());
    
                // check if current product in loop is activated
                if ($curProduct->getStatus()) {
    
                    // build list-item with classes
                    $pChild .= '<li';
                    $pChild .= ' class="type-product level'.$level;
                    if ($p == 1 && $correctClasses == 0) {
                        $pChild .= ' first';
                    }
                    if ($p == $productCount) {
                        $pChild .= ' last';
                    }
                    $pChild .= '">'."\n";
                    $pChild .= ' <a href="'.$curProduct->getProductUrl().'">'.$this->htmlEscape($curProduct->getName()).'</a>'."\n";
                    $pChild .= '</li>';
    
                    // increment product counter
                    $p++;
                }
            }
        }
    
        return $pChild;
    }
    

    希望它可以帮助某人!如果有人建议更清洁地写它或添加一些功能,发布或评论它! :)

相关问题