Php Magento按管理顺序加载类别
我正在尝试加载标记为包含在Magento导航中的所有类别,其顺序与管理面板中的顺序相同,以便构建自定义分层菜单(我们将输出与存储在DB中的另一个菜单组合到其他页面) 到目前为止,我在生成菜单的函数中使用了以下内容:Php Magento按管理顺序加载类别,php,magento,plugins,Php,Magento,Plugins,我正在尝试加载标记为包含在Magento导航中的所有类别,其顺序与管理面板中的顺序相同,以便构建自定义分层菜单(我们将输出与存储在DB中的另一个菜单组合到其他页面) 到目前为止,我在生成菜单的函数中使用了以下内容: private function generateCategories() { $_root_category_id = Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId
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;
}
专用函数生成类别(){
$\u root\u category\u id=Mage::app()->getWebsite(true)->getDefaultStore()->getRootCategoryId();
$\u current\u children=Mage::getModel('目录/类别')
->getCollection()
->addAttributeToSelect(“*”)
->addIsActiveFilter()
->addLevelFilter(2)
->addOrderField('position','asc');
$i=0;
$html='';
foreach($\当前\子项为$l0){
如果($l0->getID()!=$\u root\u category\u id&&$l0->getName()!=''&&&$l0->getIncludeInMenu()){
$i++;
如果(Mage::helper('core/url')->getCurrentUrl()==$l0->getURL())
$active='active';
其他的
$active='';
如果($l0->hasChildren())
$parent='parent';
其他的
$parent='';
$html.=';
如果($l0->hasChildren()){
$multiplier=1;
$iteration=0;
$level1='';
$level1[]='';
foreach(将(“,”,$l0->getChildren())分解为$l1){
$l1=Mage::getModel('catalog/category')->load($l1);
如果($l1->getIncludeInMenu()){
如果(Mage::helper('core/url')->getCurrentUrl()==$l1->getURL())
$active='active';
其他的
$active='';
如果($iteration==$this->perColumn){
$iteration=0;
$multiplier++;
}
$iteration++;
$level1[]='';
}
}
未设置($level1[0]);
$numLinks=计数($level1);
$columns=$numLinks/$this->perColumn;
$html.='';
$used=0;
$iteration=0;
foreach($level1作为$link){
$used++;
$iteration++;
如果($used==1)
$html.='';
$html.=$link;
如果($used==4 | |$iteration==numLinks){
$html.='';
$used=0;
}
}
$html.='';
}
$html.=' ';
}
}
返回$html;
}
我的印象是,->addOrderField('position','asc')
应该将类别过滤成与管理面板中相同的顺序,但这只适用于第一级($l0
)类别,而不是子类别
有人可以建议如何修改此项以使其正常工作吗?尝试使用函数getChildrenCategories()而不是getChildrenCategories()。这将返回一个对象,而不是类别的id,因此您无需加载信息
有关更多信息,请访问$subcategories=Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('parent_id',$categoryId)
->addAttributeToSort(“名称”,ASC)
?>谢谢,非常好用。我没有意识到它们是作为对象返回类别的一种方式,我认为它们必须在之后加载。这与名称无关!