我正在尝试以与管理面板中相同的顺序加载标记为包含在 Magento 导航中的所有类别,以构建自定义分层菜单(我们将输出与存储在数据库到其他页面)。
到目前为止,这是我在生成菜单的函数中使用的:
private function generateCategories() {
$_root_category_id = Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
$_current_children = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('*')
->addIsActiveFilter()
->addLevelFilter(2)
->addOrderField('position', 'asc');
$i = 0;
$html = '';
foreach( $_current_children as $l0 ) {
if ($l0->getID() != $_root_category_id && $l0->getName() != '' && $l0->getIncludeInMenu()) {
$i++;
if (Mage::helper('core/url')->getCurrentUrl() == $l0->getURL())
$active = ' active';
else
$active = '';
if ($l0->hasChildren())
$parent = ' parent';
else
$parent = '';
$html .= '<li class=" level0' . $active . $parent . '"><a href="' . $l0->getURL() . '" class="top">' . $l0->getName() . '</a>';
if ($l0->hasChildren()) {
$multiplier = 1;
$iteration = 0;
$level1 = '';
$level1[] = '';
foreach ( explode(',', $l0->getChildren()) as $l1 ) {
$l1 = Mage::getModel('catalog/category')->load($l1);
if ( $l1->getIncludeInMenu() ) {
if (Mage::helper('core/url')->getCurrentUrl() == $l1->getURL())
$active = ' active';
else
$active = '';
if ($iteration == $this->perColumn) {
$iteration = 0;
$multiplier++;
}
$iteration++;
$level1[] = '<span class="level1' . $active . '"><a href="' . $l1->getURL() . '" title="' . $l1->getName() . '">' . $l1->getName() . '</a></span>';
}
}
unset($level1[0]);
$numLinks = count($level1);
$columns = $numLinks / $this->perColumn;
$html .= '<div class="border-cover"></div><div class="dropdown" style="width: ' . $this->colWidth * $multiplier . 'em;">';
$used = 0;
$iteration = 0;
foreach( $level1 as $link ) {
$used++;
$iteration++;
if ($used == 1)
$html .= '<div class="col" style="float: left; width: ' . $this->colWidth . 'em;">';
$html .= $link;
if ($used == 4 || $iteration == $numLinks) {
$html .= '</div>';
$used = 0;
}
}
$html .= '</div>';
}
$html .= '</li>';
}
}
return $html;
}
我的印象是->addOrderField('position', 'asc')
应该将类别过滤到与管理面板中相同的顺序,但这仅适用于第一级 ( $l0
) 类别,不适用于子类别。
任何人都可以建议如何修改它以使其工作?