在多商店Magento安装中的单个网站的顶部导航中恢复标准Mage_Catalog_Block_Navigation功能

时间:2014-08-12 18:44:26

标签: php magento

我目前正在开发具有多个网站和商店视图的Magento安装。我正在尝试重新设计四个子网站中的一个。

添加了一个自定义模块,它似乎重写/扩展默认的顶部导航菜单以自动包含CMS页面并向菜单添加横幅广告位。问题是我想恢复单个网站视图的默认Magento顶级菜单(即没有CMS页面)。

我已经尝试禁用系统内的模块 - >配置 - >高级 - >该网站的高级,但这似乎使整个顶部导航消失。我相信我要删除的功能是:

<?php
/**
 * extend functions from navigation.php
 * 
 */
class Fvzzy_Category_Block_Navigation extends Mage_Catalog_Block_Navigation
{
    protected $_menus;

    protected function _getCategoryMenuItemHtml($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);
        $text = preg_replace('/[^A-Za-z0-9]/i', '-', strtolower($category->getName()));
        //$classes[] = $category->;
        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';
        }
        if(strtolower($text) == 'sale') $classes[] = strtolower($text);
        // 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>';

        // render children
        $htmlChildren = '';
        $j = 0;
        foreach ($activeChildren as $child) {
            $htmlChildren .= $this->_getCategoryMenuItemHtml(
                $child,
                ($level + 1),
                ($j == $activeChildrenCount - 1),
                ($j == 0),
                false,
                $outermostItemClass,
                $childrenWrapClass,
                $noEventAttributes
            );
            $j++;
        }
        $promotion = $this->addPromotions($category->getId());
        if (!empty($htmlChildren)) {
            if ($childrenWrapClass) {
                $html[] = '<div class="' . $childrenWrapClass . '">';
            }
            $html[] = '<ul class="level' . $level . '"><div class="menu-text">';
            if($promotion) $html[] = '';
            $html[] = $htmlChildren;
            if($promotion) $html[] = '';
            if($promotion) $html[] = '</div><li class="level1 parent menu-promotion">'.$promotion.'</li>';
            $html[] = '</ul>';
            if ($childrenWrapClass) {
                $html[] = '</div>';
            }
        }

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

        $html = implode("\n", $html);
        return $html;
    }

    public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
    {
        $activeCategories = array();
        foreach ($this->getStoreCategories() as $child) {
            if ($child->getIsActive()) {
                $activeCategories[] = $child;
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $hasActiveCategoriesCount = ($activeCategoriesCount > 0);

        if (!$hasActiveCategoriesCount) {
            return '';
        }

        $html = '';
        $j = 0;
        foreach ($activeCategories as $category) {
            $html .= $this->_getCategoryMenuItemHtml(
                $category,
                $level,
                ($j == $activeCategoriesCount - 1),
                ($j == 0),
                true,
                $outermostItemClass,
                $childrenWrapClass,
                true
            );
            $j++;
        }
        //main store
        if(Mage::app()->getStore()) $html .= $this->addMenu($childrenWrapClass,Mage::app()->getStore());
        return $html;
    }

    protected function addPromotions($id = 0){
        if($id){
            $base = Mage::getBaseUrl('media',true).'promotion_box_images/';
            $file_base = Mage::getBaseDir('media').'/promotion_box_images';
            $html = ''; $img = '';
            $promotion = Mage::getModel('promotion/box')->getCollection()->addFieldToFilter('menu_ids',array(
                array('like'=>$id),
                array('like'=>$id.',%'),
                array('like'=>'%,'.$id.',%'),
                array('like'=>'%,'.$id)))->setOrder('position')->getFirstItem(); //default is desc
            if($promotion && $promotion->getId()){
                $type=$promotion->getDisplayType();
                $link = ''; $img = ''; $content = ''; $width = '';
                if(file_exists($file_base.'/'.$promotion->getImage())){
                    list($width) = getimagesize($file_base.'/'.$promotion->getImage());
                }
                if($type!=null){
                    switch($type){
                        case 0:
                            if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
                            if(trim($promotion->getLink())!='') $link = $promotion->getLink();
                            break;
                        case 1:
                            if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
                            if($promotion->getCategoryId()){
                                $c = Mage::getModel('catalog/category')->load($promotion->getCategoryId());
                                if($c->getId() && $c->getIsActive()) $link = Mage::helper('catalog/category')->getCategoryUrl($c);
                            }
                            break;
                        case 2:
                            if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
                            if($promotion->getProductSku()){
                                $id = Mage::getModel('catalog/product')->getIdBySku($promotion->getProductSku());
                                $p = Mage::getModel('catalog/product')->load($id);
                                if($p->getId()) $link = $p->getProductUrl();
                            }
                            break;
                        case 3:
                            if($promotion->getImage()!='') $img = '<img src="'.$base.$promotion->getImage().'" alt="'.$promotion->getTitle().'" width="'.$width.'"/>';
                            if($promotion->getPageId()){
                                $_link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());
                                                                if($_link != ''){ 
                                                                $nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
                                                                  ->addFieldToFilter('page_id', array('eq' => $promotion->getPageId()));
                                                            foreach($nodes as $_node){
                                                                $link = $_node->getRequestUrl();
                                                            }
                                                            if(!$link){$link = Mage::Helper('cms/page')->getPageUrl($promotion->getPageId());}
                                                                }
                            }
                            break;
                        case 4:
                            $content = $promotion->getContent();
                            break;
                    }
                }
                if($img && $link){
                    $html = '<a href="'.$link.'">'.$img.'</a>';
                }
                elseif($img){
                    $html = '<a href="">'.$img.'</a>';
                }
                else{
                    $html = $content;
                }
            }
            return $html;
        }else{return false;}
    }

    //$menu = array( array('label'=>'News','href'=>'blog','module'=>'blog','sub'=>array(array('label'=>'submenu','href'=>'blog2','module'=>'blog'))) );
    public function setOtherMenu($menu){
        $this->_menus = $menu;
    }

    public function addMenu( $childrenWrapClass = '', $store ) {

            $this->_node_ids = array();
            $current_module = $this->getRequest()->getModuleName();
            $page_id = $this->getRequest()->getParam('page_id');

            // get all the cms page nodes not folder or containers
            $nodes = Mage::getModel('enterprise_cms/hierarchy_node')->getCollection()
                                                                    ->joinMetaData()
                                                                    ->addFieldToFilter( 'level', 1 )
                                                                    //->addFieldToFilter( 'menu_visibility', 1 )
                                                                    ->addFieldToFilter( 'main_table.page_id', array( 'notnull' => true ))
                                                                    ->setOrder('sort_order','ASC');

            if ( $store instanceof Mage_Core_Model_Store ) {
                $store = $store->getId();
                $storeIds = array( 0, $store );
                $nodes->getSelect()
                      ->joinLeft( array( 'cs' => 'cms_page_store' ), 'main_table.page_id=cs.page_id', array( 'cs.store_id' ) )
                      ->where( 'cs.store_id IN ('. implode( ',', $storeIds ) .')' ); // not the best sql here but it works as store id will be one at a time.
            }

            $active_node = Mage::registry('current_cms_hierarchy_node');
            $active_node_ids = array();
            if($active_node){
                $xpath = $active_node->getXpath();
                $all = explode( '/', $xpath );
                foreach ( $all as $index ) $active_node_ids[] = $index;
            }
            $this->_node_ids = $active_node_ids;
            if ( $this->_menus ) {
                $menus = $this->_menus;
            }
            else {
                $menus = array();
            }

            $links = '';
            foreach ( $nodes as $node ) {
                $tree = $node->setCollectActivePagesOnly(true)
                             ->setCollectIncludedPagesOnly(true)
                             ->setTreeMaxDepth(0)
                             ->setTreeIsBrief(1) //this way it won't show the container link
                             ->getTreeSlice(0, 1); //up to tree top and down to one level
                $links .= $this->drawCmsMenu($tree,0,1,$childrenWrapClass);
            }
            foreach ( $menus as $menu ) {
                if(isset($menu['sub'])) $has_sub_class = ' parent'; else $has_sub_class = '';
                if (isset($menu['module']) && $current_module == $menu['module'] ) {
                    $links .= '<li class="level0 level-top active'.$has_sub_class.'">';
                }
                else {
                    $links .= '<li class="level0 level-top'.$has_sub_class.'">';
                }
                $html = '';
                if(isset($menu['sub']) && is_array($menu['sub'])){
                    $submenu = $menu['sub'];
                    $html .= '<div class="'.$childrenWrapClass.'"><ul class="level0">';
                    foreach($submenu as $sub){
                         if(is_array($sub) &&isset($sub['href']) && isset($sub['label']))
                                                        //2012-11-20 AU TEAM coding for change <h2> -> <span>
                            $html .= '<li class="level1"><a href="'.Mage::getBaseUrl().$sub['href'].'"><span>'.$sub['label'].'</span></a></li>';
                    }
                    $html .= '</div>';
                }
                if(isset($menu['href']) && isset($menu['label']))
                    $links .= '<a class="level-top" href="'.Mage::getBaseUrl().$menu['href'].'"><span>'.$menu['label'].'</span></a>';
                //add submenu
                if($html != '') $links .= $html;
                $links .= '</li>';
            }
            return $links;
    }

    /* Add Maximal Depth filter
     * 2012.04.11 by Bruce
     */
    public function drawCmsMenu( array $tree, $parentNodeId = 0, $level = 0, $childrenWrapClass = '' ) {

        $html = '';

        if ( !isset($tree[$parentNodeId ])) return $html;

        foreach ( $tree[ $parentNodeId ] as $nodeId => $node ) {
            /* @var $node Enterprise_Cms_Model_Hierarchy_Node */
            $nested = $this->drawCmsMenu( $tree, $nodeId, $node->getLevel()+1, $childrenWrapClass);
            $hasChildren = ( $nested != '' );
            // set style classes
            $class = array();
            $class[] = 'level'. ( $level - 1 );
            if ( $level - 1 == 0 )
                $class[] = 'level-top';
            if ( $this->_node_ids && in_array( $node->getNodeId(), $this->_node_ids ) )
                $class[] = 'active';
            if ( $hasChildren )
                $class[] = 'parent';
            // li wrapper header
            $html .= '<li class="'. implode( ' ', $class ) .'">';
            $html .= $this->_getNodeLabel( $node, $level - 1 );
            // div wrapper header
            if ( $hasChildren ){
                if ( $childrenWrapClass ) $html .= '<div class="'. $childrenWrapClass .'">';
                $html .= '<ul class="level'. ( $level - 1 ) .'">';
            }
            // children
            $html .= $nested;
            // div wrapper footer
            if ( $hasChildren ) {
                $html.='</ul>';
                if ( $childrenWrapClass ) $html .= '</div>';
            }
            // li wrapper footer
            $html .= '</li>';
        }
        return $html;

    }

    protected function _getNodeLabel($node,$level)
    {
        if($level==0) $class = 'level-top'; else $class = '';
        if($node->getPageTitle()) return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getPageTitle().'</span></a>';
        else return '<a class="'.$class.'" href="'.$node->getUrl().'"><span>'.$node->getLabel().'</span></a>';
    }
}

我知道这个模块的配置文件如下所示:

<?xml version="1.0"?>
<config>
    <modules>
        <Fvzzy_Category>
            <version>0.1.0</version>
        </Fvzzy_Category>
    </modules>
    <global>
        <blocks>
            <fvzzy_category>
                <class>Fvzzy_Category_Block</class>
            </fvzzy_category>
            <catalog>
                <rewrite>
                    <navigation>Fvzzy_Category_Block_Navigation</navigation>
                </rewrite>
            </catalog>
        </blocks>
        <resources>
            <fvzzy_category_setup>
                <setup>
                    <module>Fvzzy_Category</module>
                    <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </setup>
            </fvzzy_category_setup>
        </resources>
    </global>
</config>

如果我能够从该文件中删除以下部分,我会得到我想要的结果:

            <catalog>
                <rewrite>
                    <navigation>Fvzzy_Category_Block_Navigation</navigation>
                </rewrite>
            </catalog>

问题是,我只想让顶部导航菜单功能恢复正常,以便一个网站视图。

  1. 有没有办法覆盖一个商店/网站视图的导航重写事件?
  2. 如果没有,是否可以禁用此模块而不禁用该网站视图的整个顶级菜单?
  3. 我尝试完全复制模块,禁用原始模块并在我想要的网站中启用新模块,但这也似乎使顶部菜单完全消失。

    我真的不确定还有什么可以做的 - 如果有人能够提供帮助,我将非常感激。

    非常感谢。

1 个答案:

答案 0 :(得分:0)

如果您希望此扩展程序仍然适用于您的Magento应用程序,则禁用该扩展程序将不会产生预期结果。

在这种情况下,你可以做两件事

  1. 使用您的模块覆盖此自定义扩展程序并根据需要进行更改

  2. 注释掉定义核心块覆盖的配置部分。你已经尝试过它了

  3. 还有另一种选择。您可以在重写块类中进行条件检查。这种情况检查有点像这样。

      if(Mage::app()->getStore()->getCode()== 'website_code_for_sub_site'){
    
            //execute existing code 
      }
    
  4. 此方法确保仅在具有代码website_code_for_sub_site的网站正在查看时才会执行在重写块类内定义的方法。

    希望有所帮助

相关问题