0

この質問のように: Magento: HOW-TO アクティブな製品をメイン ナビゲーション メニューのドロップダウンに追加する

…メインメニューのカテゴリーの商品を一覧表示したい。提供されたコードを使用しようとしましたが、まだ機能していません。私は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 つの新しい関数を作成しました。

現在のカテゴリから製品コレクションを取得する 1 つの新しい小さな関数:

// 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 を作成する 1 つの関数:

// 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 に答える