php - Long processing time on menu script -
i building out nested menu magento store working on. store has around 700 categories in total (that nested around 4 levels @ most) need spat out menu.
the code have written takes on average 2.5s process (tested using microtime).
i wondering if unavoidable given amount of categories need processed.
anyways, code have come (go easy front end dev trade): note: using code loop out cms pages in same fashion
$type = mage::registry('current_category') ? 'category' : 'page'; if($type == 'category') { $currentid = mage::registry('current_category')->getid(); $parentids = explode('/', mage::registry('current_category')->path); $rootid = mage::app()->getstore()->getrootcategoryid(); } else { $currentid = mage::getsingleton('cms/page')->getid(); $parentids = mage::getsingleton('cms/page')->getpathids(); $rootid = 0; } function checkchildhtml($parentid, $htmlstring) { $string = ''; if($parentid != $rootid) { $string = $htmlstring; } return $string; } // recurse site tree , build out menu function buildchildmenu($type, $currentid, $parentid, $ischild, $parentids, $rootid) { // appropriate collection based on type if($type == 'category') { $children = mage::getmodel('catalog/category')->getcollection() ->addattributetoselect('*') ->addattributetofilter('is_active', '1') ->addattributetofilter('include_in_menu', '1') ->addattributetofilter('parent_id', array('eq' => $parentid)) ->addattributetosort('position', 'asc'); } else { $children = mage::getmodel('cms/page')->getcollection() ->addfieldtoselect('*') ->addfieldtofilter('is_active', '1') ->addfieldtofilter('include_in_menu', '1') ->addfieldtofilter('parent_id', array('eq' => $parentid)) ->setorder('position','asc'); } // todo check $parentid != $rootid little hacky, need dry $html .= ($parentid != $rootid) ? '<ul>' : null; // loop on categories @ current level foreach($children $child) { $childid = $child->getid(); $parent = (count($child->getchildren()) > 0) ? $child->getchildren() : false; $classes = []; // build out class lists if($parent) { $classes[] = 'parent'; } if(in_array($childid, $parentids, true) || count($children) == 1) { $classes[] = "current active"; } if($childid == $currentid) { $classes[] = "current-page"; } // build out list item values appropriate type if($type == 'category') { $html .= checkchildhtml($parentid, '<li class="' . implode(' ', $classes) . '">' . ($parent ? '<button class="toggle"></button>' : null) . '<a href="' . $child->geturl() . '">' . $child->getname() . '</a>'); } else { $html .= checkchildhtml($parentid, '<li class="' . implode(' ', $classes) . '">' . ($parent ? '<button class="toggle"></button>' : null) . '<a href="' . $child->geturl() . '">' . $child->title . '</a>'); } // append list html (if not root page) if($parent) { // categories below page $html .= buildchildmenu($type, $currentid, $child->getid(), true, $parentids, $rootid); } // close list (if not root product page) $html .= checkchildhtml($parentid, '</li>'); } $html .= checkchildhtml($parentid, '</ul>'); return $html; } // build out menu root level down $categorylisthtml = buildchildmenu($type, $currentid, $rootid, false, $parentids, $rootid);
are there obvious bottlenecks here? if not, best practise in scenario?
for instance, should ajax children when requested? or maybe cache menu? or... else?
okay, issue had cache turned off whilst developing menu. cache enabled processing time insignificant.
Comments
Post a Comment