如何在Magento菜单中列出产品



喜欢这个问题:Magento:HOW-TO在主导航菜单的下拉菜单中添加活动产品

我想在主菜单中列出一个类别中的产品。我已经尝试使用提供的代码,但它仍然不起作用。我使用的是Magento版本1.7.0.2,所以我认为这可能是问题所在。

如有任何帮助,我们将不胜感激。

我遇到了同样的问题,几个小时后我现在有了解决方案。我使用的是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;
}

希望它能帮助到别人!如果有人建议写得更干净或添加一些功能,请发布一些内容或发表评论!:)

最新更新