Magento - 获取父类别和所有子子类别

发布于 2024-10-31 04:09:38 字数 376 浏览 0 评论 0原文

我有一个类别,有 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

谢谢

I have a single category, that has 2 subcategories. Within each of these categories, are 5 subcategories.

Is there a way to get a list of all of these 10 sub-sub-categories?

Thanks

EDIT:

Something like this:

  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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半步萧音过轻尘 2024-11-07 04:09:38

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

高性能单查询解决方案:

$parentCategory 成为您的主类别,然后此集合将加载所有子类别,以下两级:

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

path 字段包含以所有祖先 ID 为前缀的类别 ID,格式为 1/2/3。从数据库角度来说,它是 catalog_category_entity 中具有索引的列,因此这样的比较不会出现性能问题。

All answers so far load children categories in a loop which is generally bad practice and causes execution of many SQL queries where a single one would suffice.

Performant Single Query Solution:

Let $parentCategory be your Main Category, then this collection will load all subcategories, two levels below:

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

The path field contains the category id prefixed with all ancestor ids in the form 1/2/3. Database wise it is a column in catalog_category_entity that has an index, so comparison like this has no performance issues.

酸甜透明夹心 2024-11-07 04:09:38

想通了:

$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>';
          }
     }
  }
}

感谢您的浏览!

Figured it out:

$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>';
          }
     }
  }
}

Thanks for looking!

暗藏城府 2024-11-07 04:09:38

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

$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;
}

希望这会对您有所帮助。

I have developed a recursive function to get all children of a category

$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;
}

Hope this will help you.

人疚 2024-11-07 04:09:38
$this->getCurrentCategory()->getParentCategory()->getData();

试试这个

$this->getCurrentCategory()->getParentCategory()->getData();

try this

天生の放荡 2024-11-07 04:09:38

我所做的就是递归地查找祖先类别的 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>';
                            }
                        }
                }
            }

What I do is recursively find the id of the ancestor category.
Once found it, I can print the current category sub categories.
If the current category has no children, than I will just print the father's subcategory.
In this way you can have infinite subcategories and still being able to display a sub category list.

             $_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>';
                            }
                        }
                }
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文