Magento类别和导航产品 - 儿童

时间:2014-01-28 01:18:16

标签: php magento navigation

我正在使用某种magento导航调整,它在主导航中显示类别和产品: http://oi41.tinypic.com/317d4t4.jpg

但是有一个问题:在我的类别中我有:

Theme supplies
 -VIP Sparklers (2)
 -UV / Glow (12)
 -Seasonal (6)
 --Summer (6)
 -Confetti Cannons (7)
 -Props (2)

问题出在“Summer”子类别中,它以这种方式显示: http://oi41.tinypic.com/166he80.jpg

如何以其他方式显示它,就像其他类别一样?像这样:

SEASONAL
SUMMER
Product1
2
3
4
5
6

这是我的navigation.php

<?php
/**
 * @version   1.0 12.0.2012
 * @author    Olegnax http://www.olegnax.com <mail@olegnax.com>
 * @copyright Copyright (C) 2010 - 2012 Olegnax
 */

class Olegnax_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation
{

    /**
     * columns html
     *
     * @var array
     */
    protected $_columnHtml;

    /**
     * Render category to html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @param boolean Whether ot not this item is first, affects list item class
     * @param boolean Whether ot not this item is outermost, affects list item class
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @param boolean Whether ot not to add on* attributes to list item
     * @return string
     */
   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);

    // 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>';

    // Grabbing the products for the category if it's level is 1
    if ($level == 1) {

        $catId = $category->getId();
        $categorie = new Mage_Catalog_Model_Category();
        $categorie->load($catId); // this is category id
        $collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
        $html[] = '<ul>';

        foreach ($collection as $pc)
        {
            $p = new Mage_Catalog_Model_Product();
            $p->load($pc->getId());

            $data = $p->_data;
            $html[] = '<li><a href="/shop/'.$data['url_path'].'">'.$data['name'] .'</a></li>';
        }

        $html[] = "</ul>\n";

    }
    // Done

    // 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("\n", $html);
    return $html;
}


}

1 个答案:

答案 0 :(得分:0)

我认为this is basically what you're looking for。几年前我在一个项目中碰到了这个问题,这就是我解决它的方法。它允许在下拉菜单中轻松添加类别及其各自的产品图像。

<强>更新

你想删除这些行,但我不是肯定的:

if ($hasActiveChildren && !$noEventAttributes) {
     $attributes['onmouseover'] = 'toggleMenu(this,1)';
     $attributes['onmouseout'] = 'toggleMenu(this,0)';
}