0

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

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

任何帮助,将不胜感激。

4

1 回答 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;
}

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

于 2015-04-17T18:29:46.317 回答