我正在为我正在处理的Magento商店构建一个嵌套菜单。该商店总共有大约700个类别(最多嵌套4个级别),需要在这个菜单中吐出。
我编写的代码平均需要2.5秒才能处理(使用microtime进行测试)。
我想知道,鉴于需要处理的类别数量,这是否是不可避免的。
无论如何,这是我提出的代码(通过交易轻松我是前端开发):注意:我也使用此代码以相同的方式循环出CMS页面
$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 the site tree and build out a menu
function buildChildMenu($type, $currentID, $parentId, $isChild, $parentIDs, $rootID) {
// Get the 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 for $parentID != $rootID is a little hacky, need to DRY this up
$html .= ($parentId != $rootID) ? '<ul>' : null;
// Loop over categories at the current level
foreach($children as $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 the list item with the values appropriate to the 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 the list html (if not root page)
if($parent) {
// Get the categories below this page
$html .= buildChildMenu($type, $currentID, $child->getId(), true, $parentIDs, $rootID);
}
// Close the list (if not root product page)
$html .= checkChildHtml($parentId, '</li>');
}
$html .= checkChildHtml($parentId, '</ul>');
return $html;
}
// Build out menu from root level down
$categoryListHtml = buildChildMenu($type, $currentID, $rootID, false, $parentIDs, $rootID);
这里有明显的瓶颈吗?如果没有,在这种情况下最佳做法是什么?
例如,我应该在要求时给孩子们播放AJAX吗?或者可以缓存菜单?或者......别的什么?
答案 0 :(得分:0)
好的,问题是我在开发此菜单时关闭了缓存。启用缓存后,处理时间无关紧要。