自定义Magento导航目录

Magento的默认主题中导航目录是垂直下拉式菜单:鼠标滑过时会显示下级菜单。实际上你如果要把菜单改成水平下拉式的话,最好的方式重写目录导航的block,自定义生成你所需要的导航样式。定义目录导航的文件位于/app/code/core/Mage/Catalog/Block/Navigation.php。主要由这renderCategoriesMenuHtml和_renderCategoryMenuItemHtml两个函数来实现。

函数renderCategoriesMenuHtml取出所有一级目录,然后丢给renderCategoryMenuItemHtml去循环判断是否有子目录,如果子目录是可显示的话,才能在前台显示。其实可以由一个递归函数来实现嘛。为此,我写一个小插件来实现这个功能。

首先添加插件的模块信息app/etc/modules/Cm_Menu.xml

 <config><modules><cm_menu><active>true</active>
      <codepool>local</codepool>
      <version>1.0.0</version></cm_menu></modules></config> 

添加模块配置信息,重写了catalog/navigation这个block

 <config><modules><cm_menu><version>1.0.0</version></cm_menu></modules> 
  <global><helpers><menu>
        <class>Cm_Menu_Helper</class>
      </menu></helpers> 
    <blocks><menu>
            <class>Cm_Menu_Block</class>
        </menu>

         <catalog><rewrite><navigation>Cm_Menu_Block_Navigation</navigation></rewrite></catalog></blocks></global></config>  

自定义renderMenu函数来生成导航。Cm_Menu_Block_Navigation 继承Mage_Catalog_Block_Navigation,这样就可以使用在phtml使用自定义的函数。

getStoreCategories() as $child) {
                if ($child->getIsActive()) {
                    $activeCategories[] = $child;
                }
            }
        }else{
            if (!$category->getIsActive()) {
                return '';
            }   
            // get all children
            if (Mage::helper('catalog/category_flat')->isEnabled()) {
                $children = (array)$category->getChildrenNodes();
            } else {
                $children = $category->getChildren();
            }
            foreach ($children as $child) {
                if ($child->getIsActive()) {
                    $activeCategories[] = $child;
                }
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $hasActiveCategoriesCount = ($activeCategoriesCount > 0);
        //only check parent category 
        if($level == 0 && $category == ''){
            if (!$hasActiveCategoriesCount) {
                return '';
            }
        }
        $_html = '';
        $j = 0;
        if($level == 0 && $category == ''){
            foreach ($activeCategories as $category) {
                $_html .= $this->renderMenu( $level, $category , ($j == 0));
                $j++;
            }
            return $_html;
        }else{
            $_url = $this->getCategoryUrl($category);
            $_name = $this->escapeHtml($category->getName());
            //$_child = Mage::getModel( 'catalog/category' )->load($category->getId());
            //$_description = $_child->getDescription();
            if(!$_description){
                $_description = $_name;
            }
            $html = array();
            foreach ($activeCategories as $category) {
                $_html .= $this->renderMenu( $level + 1, $category , ($j == 0));
                $j++;
            }
            $classes = array();
            if ($isFirst) {
                $classes[] = 'fore';
            } 
            if($level == 0){
                $classes[] = 'item';
                $attributes = array();
                if (count($classes) > 0) {
                    $attributes['class'] = implode(' ', $classes);
                }
                $htmlLi = ' <div'; foreach="" ($attributes="" as="" $attrname="">$attrValue) {
                    $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
                }
                $htmlLi .= '>';
                $html[] = $htmlLi;

                $html[] = '<span>';
                $html[] = '

### ';
                $html[] = '['.$_name.']('.$_url.' "'.$_description.'")';
                $html[] = '

';
                $html[] = '</span>';
                $html[] = '

<div class="i-mc">';
                $html[] = '

<div class="subitem">';
                $html[] = $_html;   
                $html[] = '</div>

';
                $html[] = '</div>

';
                $html[] = '';
            }
            if($level == 1){
                $attributes = array();
                if (count($classes) > 0) {
                    $attributes['class'] = implode(' ', $classes);
                }
                $htmlLi = ' <dl'; foreach="" ($attributes="" as="" $attrname="">$attrValue) {
                    $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
                }
                $htmlLi .= '>';
                $html[] = $htmlLi;

                $html[] = '

<dt>';
                $html[] = '['.$_name.']('.$_url.' "'.$_description.'")';
                $html[] = '</dt>

';
                $html[] = '

<dd>';
                $html[] = $_html;
                $html[] = '</dd>

';
                $html[] = '';
            }
            if($level == 2){
                $html[] = '_';
                $html[] = '['.$_name.']('.$_url.' "'.$_description.'")';
                $html[] = '_';
            }
            $_html = implode("\n", $html);
            return $_html;
        }
    }
}
?></dl';></div';> 

在renderMenu函数中,函数有3个参数,$level,$category,$j。$level参数是目录的层级,参数$category是子目录的实例。目录的层级限制在3层以内,在第一次循环的的时候,$level=0,会从系统取出全部的一级目录分类,存入$activeCategories数组中,通过计算$activeCategories数组的个数来判断时候是否存在二级目录。如果存在二级目录,再丢给renderMenu函数自己,此时$level=0,$category参数是子目录的实例,根据不同的$level进行循环,得到最后的目录列表。

将block的type换成

在模板中只需要调用renderMenu函数

renderMenu();?>

本插件在Magento CE 1.6.2 上测试通过

“自定义Magento导航目录”的2个回复

发表评论

电子邮件地址不会被公开。 必填项已用*标注