1

I am trying to improve the store speed and I found that main Menu is very slow. I want to make magento do an ajax call for calling the menu, but I cannot understand how should work that.

<?php $_menu = $this->renderCategoriesMenuHtml(FALSE, 0,'level-top'); $theme = $this->helper('megamenu'); ?>

<div class="nav">
<ul id="nav" class="grid-full"> 
<?php echo $_menu;?>
</ul>
</div>

I am using a custom Megamenu module.

CategoryController.php

      include_once(Mage::getModuleDir('controllers','Mage_Adminhtml').DS.'Catalog'.DS.'CategoryController.php');
class Cmsmart_Megamenu_Catalog_CategoryController extends   Mage_Adminhtml_Catalog_CategoryController
  {
        /**
     * Category save
    */
      public function saveAction()
{

    if (!$category = $this->_initCategory()) {
        return;
    }

    $storeId = $this->getRequest()->getParam('store');
    $refreshTree = 'false';
    if ($data = $this->getRequest()->getPost()) {
        $category->addData($data['general']);
        if (!$category->getId()) {
            $parentId = $this->getRequest()->getParam('parent');
            if (!$parentId) {
                if ($storeId) {
                    $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
                }
                else {
                    $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
                }
            }
            $parentCategory = Mage::getModel('catalog/category')->load($parentId);
            $category->setPath($parentCategory->getPath());
        }

        /**
         * Process "Use Config Settings" checkboxes
         */
        if ($useConfig = $this->getRequest()->getPost('use_config')) {
            foreach ($useConfig as $attributeCode) {
                $category->setData($attributeCode, null);
            }
        }

        /**
         * Create Permanent Redirect for old URL key
         */
        if ($category->getId() && isset($data['general']['url_key_create_redirect']))
        // && $category->getOrigData('url_key') != $category->getData('url_key')
        {
            $category->setData('save_rewrites_history', (bool)$data['general']['url_key_create_redirect']);
        }

        $category->setAttributeSetId($category->getDefaultAttributeSetId());

        if (isset($data['category_products']) &&
            !$category->getProductsReadonly()) {
            $products = array();
            parse_str($data['category_products'], $products);
            $category->setPostedProducts($products);
        }

        Mage::dispatchEvent('catalog_category_prepare_save', array(
            'category' => $category,
            'request' => $this->getRequest()
        ));

        /**
         * Proceed with $_POST['use_config']
         * set into category model for proccessing through validation
         */
        $category->setData("use_post_data_config", $this->getRequest()->getPost('use_config'));

        try {
            $validate = $category->validate();
            if ($validate !== true) {
                foreach ($validate as $code => $error) {
                    if ($error === true) {
                        Mage::throwException(Mage::helper('catalog')->__('Attribute "%s" is required.', $category->getResource()->getAttribute($code)->getFrontend()->getLabel()));
                    }
                    else {
                        Mage::throwException($error);
                    }
                }
            }

            /**
             * Check "Use Default Value" checkboxes values
             */
            if ($useDefaults = $this->getRequest()->getPost('use_default')) {
                foreach ($useDefaults as $attributeCode) {
                    $category->setData($attributeCode, false);
                }
            }

            /**
             * Unset $_POST['use_config'] before save
             */
            $category->unsetData('use_post_data_config');

            $category->save();
            Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('The category has been saved.'));
            $refreshTree = 'true';
        }
        catch (Exception $e){
            $this->_getSession()->addError($e->getMessage())
                ->setCategoryData($data);
            $refreshTree = 'false';
        }
    }

    //Mage::log($category->getId(), null, 'events-custom.log', true);
    // create code at here.


    $url = $this->getUrl('*/*/edit', array('_current' => true, 'id' => $category->getId()));
    $this->getResponse()->setBody(
        '<script type="text/javascript">parent.updateContent("' . $url . '", {}, '.$refreshTree.');</script>'
    );
}


}

Block

Navigation.php

       class Cmsmart_Megamenu_Block_Navigation extends    Mage_Catalog_Block_Navigation
{

 public function _prepareLayout()
{


    $layout = $this->getLayout();
    $this->setTemplate('cmsmart/megamenu/topmenu.phtml');


}

  protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
    $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
{



    if (!$category->getIsActive()) {
        return '';
    }
    $html = array();
    if (Mage::helper('catalog/category_flat')->isEnabled()) {
        $children = (array)$category->getChildrenNodes();
        $childrenCount = count($children);
    } else {
        $children = $category->getChildren();
        $childrenCount = $children->count();
    }
    $hasChildren = ($children && $childrenCount);
    $activeChildren = array();

    foreach ($children as $child) {
        if ($child->getIsActive()) {
            $activeChildren[] = $child;
        }

    }


    $classes = array();
    $classes[] = 'level' . $level;
    if ($this->isCategoryActive($category)) {
        $classes[] = 'active';
    }
    $linkClass = '';


    if (count($activeChildren) > 0) {
        $classes[] = 'parent';
    }



    $attributes = array();
    if (count($classes) > 0) {
        $attributes['class'] = implode(' ', $classes);
    }


    $htmlLi = '<li ';



    foreach ($attributes as $attrName => $attrValue) {                       
        $htmlLi .= ' ' . $attrName .  '="' . str_replace('"', '\"', $attrValue) . '"';
              } 

    if($level == 1) {
            $idc =  $category->parent_id;

            $htmlLi .= '>';    


    } else {
        $htmlLi .= '>';    
    }
    $html[] = $htmlLi;
    $id = $category->getId();
    $megamenu = Mage::helper('megamenu')->Megamenu($id);

    if($level == 1){            
    $html[] = '<a class="catagory-level1" href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>'.$this->escapeHtml($category->getName()).'</a>'; 


    } else {
    $html[] = '<a href="'.$this->getCategoryUrl($category).'"' .$linkClass.'><i class="'.$megamenu[0]['label'].'" aria-hidden="true"></i>' . $this->escapeHtml($category->getName()).'</a>';


    }



    // render children
    $htmlChildren = '';
    $j = 0;
    foreach ($activeChildren as $child) {
        $htmlChildren .= $this->_renderCategoryMenuItemHtml(
            $child,
            ($level + 1),
            ($j == 0),
            false,
            $outermostItemClass,
            $childrenWrapClass,
            $noEventAttributes
        );

        $j++;
    }




    if ($childrenWrapClass) {
       $html[] = '<div class="' . $childrenWrapClass.'">';

        }
        $html[] = '<ul class="level-main' . $level .' column'.$this->getLevle($id).'">';   


        if($level == 0) {

        $html[] = '<ul class="level' . $level .'">';


          if(@$megamenu[0]['category_children']==0)   {   
            if($htmlChildren) {
                if($level == 0){

                    $html[] = '<div class="catagory_children">'.$htmlChildren.'</div>'; 

                } else {
                    $html[] = '<div class="catagory_children column'.$this->getLevle($id).'">'.$htmlChildren.'</div>';    
                }
             }
           }
        }  else{

            $html[] = '<ul class="level' . $level . '">';
            if(@$megamenu[0]['category_children']==0)   {   
                if($htmlChildren) {
               $html[] = '<div class="catagory_children column'.$this->getLevle($id).'">'.$htmlChildren.'</div>';   


                 }
               }

        }                               
        $html[] = '</ul>'.$this->getBlockBottom($megamenu);

        $html[] = '</ul>';
    if ($childrenWrapClass) {
            $html[] = '</div>';
        }
    $html[] = '</li>';
    $html = implode("\n", $html);
    return $html;
} 




public function getBlockBottom($block){
    $active_static_block = @$block[0]['active_static_block'];
    $active_static_block_bottom = @$block[0]['active_static_block_bottom']; 
    $idblock = @$block[0]['static_block_bottom'];
    $showblock = $this->getShowblock($active_static_block_bottom,$idblock);
    if($active_static_block == 1){
        return $showblock;
    }else {
        return ;
    } 
}


public function getShowblock($active,$id) {
    if($active == 1){
        return '<ul class="static-block">'.$this->getLayout()->createBlock('cms/block')->setBlockId($id)->toHtml().'</ul>';
    }   else {
        return ;
    }     
}


public function getLevle($id){
    $megamenu = Mage::helper('megamenu')->Megamenu($id);
     if(@$megamenu[0]['level_column_count'] != 0){
         return @$megamenu[0]['level_column_count'];
     } else {
        $level = 1;
        return $level;
     } 
}



 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.