magento按字母顺序排序子类别

时间:2016-04-05 08:55:26

标签: php magento helper

我想按字母顺序排序我的子类别 我是通过改变核心来做到的,但如果我能干净利落地做到这一点。

我正在使用: $_category->getChildrenCategories();

在档案中: app/code/core/Mage/Catalog/Model/Resource/Category.php

protected function _getChildrenCategoriesBase($category)

       // ->setOrder('position', Varien_Db_Select::SQL_ASC)
       ->setOrder('name', Varien_Db_Select::SQL_ASC)

如果有人使用帮助者或其他方式。

感谢。

3 个答案:

答案 0 :(得分:0)

请尝试以下代码

<?php 

  $subcats = Mage::getModel('catalog/category')->load('1')->getChildren();
  $catIds = explode(',',$subcats );

  $catarray= array();
  foreach($catIds as $catId) {
    $category = Mage::getModel('catalog/category')->load($catId); 
    $catarray[$category->getName()] = $category->getUrl();
  }

  ksort($catarray, SORT_STRING);
?>

<ul>
   <?php foreach($catarray as $catname => $caturl): ?>
    <li>
      <a href="<?php echo $caturl; ?>"><?php echo $catname ; ?></a>
    </li>
   <?php endforeach; ?>
 </ul>

答案 1 :(得分:0)

两个提示:

  1. 您可以在本地模块中重写资源模型,并在您的案例中更改代码。

  2. 或者创建一个类似的新助手方法:

  3.  
    
        $collection = $category->getCollection();
        $collection->addAttributeToSelect('url_key')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('all_children')
                ->addAttributeToSelect('is_anchor')
                ->setOrder('name', Varien_Db_Select::SQL_ASC)
                ->joinUrlRewrite()
    
        $collection->addAttributeToFilter('is_active', 1)
                ->addIdFilter($category->getChildren())
                ->load();
        return $collection; 
    
    

答案 2 :(得分:0)

感谢Dhaval Patel和mondev。我通过调用集合

来修复它
      $currentCategory = Mage::getModel('catalog/category')->load($_category->getId());
      $_subcategories = $currentCategory->getCollection();
      $_subcategories->addAttributeToSelect('url_key')
          ->addAttributeToSelect('name')
          ->addAttributeToSelect('all_children')
          ->addAttributeToFilter('is_active', 1)
          ->addIdFilter($currentCategory->getChildren())
          ->setOrder('name', Varien_Db_Select::SQL_ASC)
          ->load();