Magento - 如何在顶层菜单中显示自定义菜单

时间:2015-08-04 09:15:13

标签: magento

我需要在顶级菜单上仅显示第一级别类别及其产品,以取代Magento的默认顶级菜单。

例如,类别和产品应显示为:

Category1
    Product name1
    Product name2
    Product name3
    Product name4
Category2
    Product name1
    Product name2
    Product name3
    Product name4

在块类中生成的菜单html结构,它将直接在phtml中使用。

由于在商店中仅使用第一级别类别,因此不需要类别树结构。

任何人都有这个任务的想法吗?

1 个答案:

答案 0 :(得分:0)

您需要重写导航和顶级菜单Magento块。

以下是示例摘录。

<强> config.xml中

<?xml version="1.0"?>
<config>
  <modules>
    <Abc_CustomMenu>
      <version>0.1.0</version>
    </Abc_CustomMenu>
  </modules>
  <frontend>
    <routers>
      <custommenu>
        <use>standard</use>
        <args>
          <module>Abc_CustomMenu</module>
          <frontName>custommenu</frontName>
        </args>
      </custommenu>
    </routers>
    <layout>
      <updates>
        <custommenu>
          <file>custommenu.xml</file>
        </custommenu>
      </updates>
    </layout>
  </frontend>
  <global>
    <helpers>
      <custommenu>
        <class>Abc_CustomMenu_Helper</class>
      </custommenu>
    </helpers>
    <blocks>
      <catalog>
        <rewrite>
          <navigation>Abc_CustomMenu_Block_Navigation</navigation>
        </rewrite>
      </catalog>
      <page>
        <rewrite>
          <html_topmenu>Abc_CustomMenu_Block_Topmenu</html_topmenu>
        </rewrite>
      </page>
      <custommenu>
        <class>Abc_CustomMenu_Block</class>
      </custommenu>
    </blocks>
  </global>
  <default>
    <custommenu_sec1>
      <custommenu_grp1>
        <custommenu_enabled>1</custommenu_enabled>
        <custommenu_items_per_col>5</custommenu_items_per_col>
      </custommenu_grp1>
    </custommenu_sec1>
  </default>
</config>

<强>的system.xml

<?xml version="1.0"?>
<config>
  <tabs>
    <Abc translate="label" module="custommenu">
      <label>Abc</label>
      <sort_order>100</sort_order>
    </Abc>
  </tabs>
  <sections>
    <custommenu_sec1  translate="label" module="custommenu">                    
      <label>Custom Menu</label>
      <tab>Abc</tab>
      <frontend_type>text</frontend_type>
      <sort_order>310</sort_order>
      <show_in_default>1</show_in_default>
      <show_in_website>1</show_in_website>
      <show_in_store>1</show_in_store>            
      <groups>
        <custommenu_grp1 translate="label"> 
          <label>General</label>
          <frontend_type>text</frontend_type>
          <sort_order>0</sort_order>
          <show_in_default>1</show_in_default>
          <show_in_website>1</show_in_website>
          <show_in_store>1</show_in_store>
          <fields>
            <custommenu_enabled translate="label">
              <label>Enabled</label>
              <frontend_type>select</frontend_type>
              <source_model>adminhtml/system_config_source_yesno</source_model>
              <sort_order>0</sort_order>
              <show_in_default>1</show_in_default>
              <show_in_website>1</show_in_website>
              <show_in_store>1</show_in_store>
            </custommenu_enabled>                        
          </fields>
        </custommenu_grp1>
      </groups>
    </custommenu_sec1>
  </sections>
</config>

<强> custommenu.xml

<?xml version="1.0"?>   
<layout version="0.1.0">
  <default>
    <block type="custommenu/toggle"></block>            
  </default>
</layout>

<强> Toggle.php

class Abc_CustomMenu_Block_Toggle extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {
        if (!Mage::getStoreConfig('custommenu_sec1/custommenu_grp1/custommenu_enabled')) return;
        $layout = $this->getLayout();
        $topnav = $layout->getBlock('catalog.topnav');
        if (is_object($topnav)) {
            $topnav->setTemplate('custommenu/top.phtml');
            $head = $layout->getBlock('head');            
            $head->addItem('skin_js', 'js/custommenu/custommenu.js');
        }
    }
}

<强> top.phtml

// your custom code here to print category and its products as menu
$categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*')//or you can just add some attributes
    ->addAttributeToFilter('level', 2)//2 is actually the first level
    ->addAttributeToFilter('is_active', 1)//if you want only active categories
;
foreach($categories as $cat)
{
    //get products from $cat category
}

<强> Navigation.php

class Abc_CustomMenu_Block_Navigation extends Mage_Catalog_Block_Navigation
{

}

<强> Topmenu.php

if (!Mage::getStoreConfig('custommenu_sec1/custommenu_grp1/custommenu_enabled')) {
    class Abc_CustomMenu_Block_Topmenu extends Mage_Page_Block_Html_Topmenu {}
    return;
}

class Abc_CustomMenu_Block_Topmenu extends Abc_CustomMenu_Block_Navigation {}