Magento - 侧栏中的类别

时间:2012-06-12 04:12:53

标签: php html magento xhtml

制作了我自己的骨架模板。我想将类别列表从标题移动到名为'left_container'的div。我怎样才能做到这一点?我正在使用Magento 1.7。

这种问题已被多次询问,但似乎每次Magento更新,实现这一目标的旧方法都行不通。

1 个答案:

答案 0 :(得分:0)

您需要创建一个预先显示所有类别的自定义菜单侧边栏,您可以下载下一个扩展程序以举例说明所有类别。这个扩展有一个递归方法来获取所有子结构:

http://www.magentocommerce.com/magento-connect/vertical-navigation-with-css-classes.html

我使用此扩展程序使自定义导航顶部按自定义属性排序,对我来说非常有用,我希望能为您提供帮助。

帮助我进行扩展的代码是下一个:

 public function drawOpenCategoryItem($category, $level=0, array $levelClass=null)
{
    $html = array();

    if ($this->_checkLoginCatalog()) return '';
    if (! $category->getIsActive()) return '';

    if (! isset($levelClass)) $levelClass = array();
    $combineClasses = array();

    $combineClasses[] = 'level' . $level;
    if ($this->_isCurrentCategory($category))
    {
        $combineClasses[] = 'active';
    }
    else
    {
        $combineClasses[] = $this->isCategoryActive($category) ? 'parent' : 'inactive';
    }
    $levelClass[] = implode('-', $combineClasses);

    $levelClass = array_merge($levelClass, $combineClasses);

    $levelClass[] =  $this->_getClassNameFromCategoryName($category);

    $productCount = '';
    if ($this->displayProductCount())
    {
        $n = $this->_getProductCount($category);
        $productCount = '<span class="product-count"> (' . $n . ')</span>';
    }

    // indent HTML!
    $html[1] = str_pad ( "", (($level * 2 ) + 4), " " ).'<span class="vertnav-cat"><a href="'.$this->getCategoryUrl($category).'"><span>'.$this->htmlEscape($category->getName()).'</span></a>'.$productCount."</span>\n";

    $autoMaxDepth = Mage::getStoreConfig('catalog/vertnav/expand_all_max_depth');
    $autoExpand = Mage::getStoreConfig('catalog/vertnav/expand_all');

    if (in_array($category->getId(), $this->getCurrentCategoryPath())
        || ($autoExpand && $autoMaxDepth == 0)
        || ($autoExpand && $autoMaxDepth > $level+1)
    ) {
        $children = $this->_getCategoryCollection()
            ->addIdFilter($category->getChildren());

        $children = $this->toLinearArray($children);

        //usort($children, array($this, '_sortCategoryArrayByName'));

        $hasChildren = $children && ($childrenCount = count($children));
        if ($hasChildren)
        {
            $children = $this->toLinearArray($children);
            $htmlChildren = '';

            foreach ($children as $i => $child)
            {
                $class = array();
                if ($childrenCount == 1)
                {
                    $class[] = 'only';
                }
                else
                {
                    if (! $i) $class[] = 'first';
                    if ($i == $childrenCount-1) $class[] = 'last';
                }
                if (isset($children[$i+1]) && $this->isCategoryActive($children[$i+1])) $class[] = 'prev';
                if (isset($children[$i-1]) && $this->isCategoryActive($children[$i-1])) $class[] = 'next';
                $htmlChildren.= $this->drawOpenCategoryItem($child, $level+1, $class);
            }

            if (!empty($htmlChildren))
            {
                $levelClass[] = 'open';

                // indent HTML!
                $html[2] = str_pad ( "", ($level * 2 ) + 2, " " ).'<ul>'."\n"
                        .$htmlChildren."\n".
                        str_pad ( "", ($level * 2 ) + 2, " " ).'</ul>';
            }
        }
    }

    // indent HTML!
    $html[0] = str_pad ( "", ($level * 2 ) + 2, " " ).sprintf('<li class="%s">', implode(" ", $levelClass))."\n";

    // indent HTML!
    $html[3] = "\n".str_pad ( "", ($level * 2 ) + 2, " " ).'</li>'."\n";

    ksort($html);
    return implode('', $html);
}