简体   繁体   中英

Get Child categories magento

Trying to get child of a specific category which is active. Please help. I am having trouble doing it. I'm currently able to show them all but not specifically. Would appreciate any help.

$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();

here is code to load active category

/* Load category by id*/
$cat = Mage::getModel('catalog/category')->load($id);


/*Returns comma separated ids*/
$subcats = $cat->getChildren();

//Print out categories string
#print_r($subcats);

foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive())
  {
    $caturl     = $_category->getURL();
    $catname     = $_category->getName();
    if($_category->getImageUrl())
    {
      $catimg     = $_category->getImageUrl();
    }
    echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
  }
}
?>

hope this is sure help you.

As mentioned by mhaupt, it is faster to load a collection rather than each category in a loop. But, as far as I am concerned, there is no need to manually load the child categories. Basically this is what $category->getChildrenCategories() already does.

There is also a filter to get active categories only. Just call addIsActiveFilter() on the collection.

a.) Load active child categories via getChildren()

// 1. Get a list of all child category ids (e.g "12,23,11,42")
$subcategoryIds = $category->getChildren();

// 2. Create collection
$categoryCollection = Mage::getModel('catalog/category')->getCollection();

// 3. Add all attributes to select, otherwise you can not 
//    access things like $cat->getName() etc.
$categoryCollection->addAttributeToSelect('*');

// 4. Filter by ids
$categoryCollection->addIdFilter($subcategoryIds);

// 5. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

b.) Load active child categories with getChildrenCategories()

// 1. Load collection
$categoryCollection= $category->getChildrenCategories();

// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();

The collection will be loaded form the database as soon as it is accessed. If the collection is not loaded and $subcategories->count() is called only a "SELECT count(*)" will be fired against the database (in contrast to count($subcategories) which will force the collection to load itself).

Iterating the collection

foreach($categoryCollection as $category) {
    echo $category->getName();
}

If you add more filters to the collection after accessing it, the collection will not load itself again automatically. To apply changes to the collection, just call $categoryCollection->load() to reload the collection from the database.

Those who are saying to use getAllChildren() instead of getChildren() are simply wrong. Both methods return the exact same thing, with one difference, getAllChildren(true) will return an array instead of a comma delimited string. getAllChildren($bool asArray) defaults to false. My point being that either way you're going to have to use

Mage::getModel('catalog/category')->load($catId);

inside of a loop unless you use the function below.

private function fetchCatsById($onlyThese)
{
    $cats = Mage::getModel('catalog/category')
                ->getCollection(true)
                ->addAttributeToSelect('*')
                ->addIdFilter($onlyThese)
                ->addAttributeToFilter('level','2')
                ->addIsActiveFilter();

    return $cats;
}

$cats = $this->fetchCatsById($onlyThese);

The one answer liyakat wrote, should not be used in professional shops, because it raises a performance issue, because of the multiple n time loads of the category object, rather use the collection of categories for that, get all children

$cat->getAllChildren()

, then limit the category collection by the needed category ids like

$coll->addIdFilter($idFilter);

then you won't have to load n times against the database.

Please do keep in mind that loads within loops are one of the most often used bad code examples in any Magento projects and to avoid them!

Hello you will see below code

$category_model = Mage::getModel('catalog/category'); 
  $_category = $category_model->load(13); 
  $all_child_categories = $category_model->getResource()->getAllChildren($_category);
  print_r($all_child_categories);

如果你想要任何数量的父类别子类别而不是点击这里http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM