【问题标题】:Magento - get a parent category and all sub-sub-categoriesMagento - 获取父类别和所有子子类别
【发布时间】:2011-07-30 16:41:48
【问题描述】:

我只有一个类别,它有 2 个子类别。在这些类别中的每一个中,都有 5 个子类别。

有没有办法获得所有这 10 个子子类别的列表?

谢谢

编辑:

类似这样的:

  Main Category
       Sub_Cat_1
            Cat_1
            Cat_2
            Cat_3
       Sub_Cat_2
            Cat_4
            Cat_5
            Cat_6

  Wanting output like:

  Cat_1
  Cat_2
  Cat_3
  Cat_4
  Cat_5
  Cat_6

谢谢

【问题讨论】:

    标签: php magento magento-1.4


    【解决方案1】:

    想通了:

    $cat = Mage::getModel('catalog/category')->load(24);
    $subcats = $cat->getChildren();
    
    foreach(explode(',',$subcats) as $subCatid)
    {
      $_category = Mage::getModel('catalog/category')->load($subCatid);
      if($_category->getIsActive()) {
        $sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
        $sub_subcats = $sub_cat->getChildren();
        foreach(explode(',',$sub_subcats) as $sub_subCatid)
        {
              $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
              if($_sub_category->getIsActive()) {
                  echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>';
              }
         }
      }
    }
    

    感谢收看!

    【讨论】:

    • 你不应该在循环中使用“load()”。
    【解决方案2】:
    $this->getCurrentCategory()->getParentCategory()->getData();
    

    试试这个

    【讨论】:

      【解决方案3】:

      我所做的是递归查找祖先类别的 id。 一旦找到它,我可以打印当前类别子类别。 如果当前类别没有孩子,那么我将只打印父亲的子类别。 通过这种方式,您可以拥有无​​限的子类别,并且仍然能够显示子类别列表。

                   $_helper = Mage::helper("catalog/category"); 
                   $rootCat = Mage::app()->getStore()->getRootCategoryId();
                   $current = Mage::registry('current_category');
      
                  show_cat($current,$_helper,$rootCat);
      
                  function show_cat($child,$_helper,$rootCat){
      
                       if ($child){
                          //look for anchestor 
                          $parentid = $child->getParentId();
                          $parent = Mage::getModel("catalog/category")->load($parentid); 
                          if($parentid != $rootCat)
                          {   
                              //find the anchestor
                              show_cat($parent,$_helper,$rootCat);
                          }else{
                              //is root 
                              $_subcategories = $parent->getChildrenCategories();
                              if(count($_subcategories)>0){
                                  echo '<ul>';
                                          foreach($_subcategories as $_category){
                                              echo '<li>';
                                              echo '<a href="'.$_helper->getCategoryUrl($_category).'">'.$_category->getName().'</a>';
      
                                                  if($child->getId() == $_category->getId()){
                                                      $current = Mage::registry('current_category');
                                                      if ($current){
                                                          //handle current
                                                          $_current_subcategories = $current->getChildrenCategories();
                                                              if(count($_current_subcategories)>0){
                                                                  //the current cat has childrens
                                                                  echo '<ul>';
                                                                  foreach($_current_subcategories as $_sub_category){
                                                                      echo '<li>';
                                                                      echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>';
                                                                      echo '</li>';
                                                                  }
                                                                  echo '</ul>';
                                                              }else{
                                                                  //the current cat has no childrens
                                                                  $current_parent = $current->getParentId();
                                                                  $current_parent  = Mage::getModel("catalog/category")->load($current_parent ); 
                                                                  $_current_subcategories = $current_parent ->getChildrenCategories();
                                                                  echo '<ul>';
                                                                      foreach($_current_subcategories as $_sub_category){
                                                                          echo '<li>';
                                                                          echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>';
                                                                          echo '</li>';
                                                                      }
                                                                  echo '</ul>';
                                                                  }
                                                          }
                                                      }
                                              echo '</li>';
                                              }
                                      echo '</ul>';
                                  }
                              }
                      }
                  }
      

      【讨论】:

        【解决方案4】:

        我开发了一个递归函数来获取一个类别的所有孩子

        $categoryObject = Mage::getModel('catalog/category')->load(CATID);
        $children = MODEL_OBJECT->getChildCategories($categoryObject);
        
        // IN MODLE FILE
        public $_catIds = array();
        
        public function getChildCategories($categoryObject){
            $categories = $categoryObject->getChildrenCategories();
            foreach ($categories as $catgory){
                if($catgory->hasChildren()){
                    $this->getChildCategories($catgory);
                }
                    $this->_catIds[] = $catgory->getId();
            }
            return $this->_catIds;
        }
        

        希望这会对你有所帮助。

        【讨论】:

          【解决方案5】:

          到目前为止,所有答案都在循环中加载子类别,这通常是不好的做法,并且会导致执行许多 SQL 查询,而单个查询就足够了。

          高性能单查询解决方案:

          $parentCategory成为你的主分类,那么这个集合将加载所有子分类,下面两个级别:

          $subcategoryCollection = Mage::getModel('catalog/category')
              ->getCollection()
              ->addFieldToFilter('level', $parentCategory->getLevel() + 2)
              ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '/%']);
          

          path 字段包含类别 ID,前缀为 1/2/3 形式的所有祖先 ID。在数据库方面,它是 catalog_category_entity 中具有索引的列,因此这样的比较没有性能问题。

          【讨论】:

          • '/%' - 正确,而不是 '%'。对于 ../8 和 ../868 在以前的版本中得到错误的结果。
          • 在遍历结果时,为什么 getUrl() 有效但 getName() 无效。获取每个子类别名称的最佳方法是什么?
          • 如果看不到名字,尝试在getCollection()后面加上addAttributeToSelect("name")
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-02
          • 2014-07-09
          • 2012-12-26
          • 1970-01-01
          • 2015-02-14
          相关资源
          最近更新 更多