使用observer magento 1.9将nth level cms页面添加到顶层菜单

时间:2015-08-06 11:05:18

标签: magento drop-down-menu submenu magento-1.9

我试图将cms页面添加到topnavigation但它只显示两个级别我想让它像magento默认渲染menu.i尝试下面的代码将cms页面添加到顶部导航但是如果有父级的子菜单那么如何使用递归方法将该子项添加到父级。

public function addItemsToTopmenuItems($observer)
{
 $menu = $observer->getMenu();
$tree = $menu->getTree();

$node = new Varien_Data_Tree_Node(array(
        'name'   => 'Categories',
        'id'     => 'categories',
        'url'    => Mage::getUrl(), // point somewhere
), 'id', $tree, $menu);

$menu->addChild($node);

// Children menu items
$collection = Mage::getModel('cms/page')->getCollection()
          ->addFieldToFilter('is_active',1)
          ->addFieldToFilter('identifier',array(array('nin'=>array('no-route','enable-cookies'))));

foreach ($collection as $category) {
    $tree = $node->getTree();
    $data = array(
        'name'   => $category->getTitle(),
        'id'     => 'category-node-'.$category->getId(),
        'url'    => Mage::getUrl($category->getIdentifier()),
    );

    $subNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);
    $node->addChild($subNode);
}
}

1 个答案:

答案 0 :(得分:1)

本周我在你的情况,最后我使用树节点实现了解决方案。这是解决方案:

import glob
directory = 'path/to/directory' + '*.dat'

files = glob.glob(directory)

for currFile in files:
    function1(currFile)
    function2(currFile)
    # insert whatever else you want to do with currFile here.

getAllChilds功能:

public function addItemsToTopmenuItems($observer)
{
        /** @var Varien_Data_Tree_Node */
        $menu = $observer->getMenu();
        /** @var  Varien_Data_Tree*/
        $tree = $menu->getTree();

        /** @var Varien_Data_Tree_Node_Collection */
        $menuCollection = $menu->getChildren();

        /** @var array of Varien_Data_Tree_Node*/
        $nodes = $menuCollection->getNodes();


        //Get "inlcude in navigation menu" parent CMS pages
        $collection = Mage::getModel('cms/page')->getCollection()
            ->addFieldToFilter('include_in_nav',true)
            ->addFieldToFilter('is_active',true)
            ->addFieldToFilter('parent_id', array(array('null' => true), array('eq' => 0)))
            ->setOrder('sort_order');


        $cmsHelper=Mage::helper('cms/page');

        foreach ($collection as $item) {

            $node = new Varien_Data_Tree_Node(array(
                'name'   => $item->getData('title'),
                'id'     => $item->getData('identifier'),
                'url'    => $cmsHelper->getPageUrl($item->getData('page_id')), // point somewhere
            ), 'id', $tree, $menu);


            $menu->addChild($node);

            $parent_id = $item->getData('page_id');
            $this->getAllChilds($node,$parent_id);
        }
    }

但是你需要在cms表中有一些额外的数据,比如'parent_id'字段。这是我的mysql4-install-0.1.0.php文件:

private function getAllChilds($node,$parent, $defaultNode = null){
    $cmsHelper=Mage::helper('cms/page');

    $pages = Mage::getModel('cms/page')
        ->getCollection()
        ->addFieldToSelect('title')
        ->addFieldToSelect('page_id')
        ->addFieldToSelect('identifier')
        ->addFieldToFilter('parent_id', $parent)
        ->addFieldToFilter('is_active', 1)
        ->load();

    $tree = $node->getTree();

    if(count($pages) > 0) {
        foreach ($pages as $item){
            $data = array(
                'name' => $item->getData('title'),
                'id' => $item->getData('identifier'),
                'url' => $cmsHelper->getPageUrl($item->getData('page_id')),
            );

            $subNode = new Varien_Data_Tree_Node($data, 'id', $tree, $node);
            $node->addChild($subNode);

            $node = $this->getAllChilds($subNode,$item->getData('page_id'), $node);
        }
        $node = $this->getAllChilds($subNode,$item->getData('page_id'));
    }

    $childNode = $node;
    if($defaultNode !== null)
        $childNode = $defaultNode;

    return $childNode;
}

最后,您需要将这些字段添加到CMS / Pages后端菜单:https://www.atwix.com/magento/adding-custom-attribute-to-a-cms-page/

祝你好运!