Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Child categories magento

Tags:

php

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();
like image 645
Tonzkie Avatar asked Aug 30 '13 05:08

Tonzkie


People also ask

How do I get child categories in Magento 2?

Just call addIsActiveFilter() on the collection. // 1. Load collection $categoryCollection= $category->getChildrenCategories(); // 2. Add filter to collection to get active categories only $categoryCollection->addIsActiveFilter();

How do I display Magento two subcategories with an image on a category page?

Find the parent category that you want to show subcategories on, then go to the 'Display Settings' tab. Switch 'Display Mode' to either 'Static block only' or 'Static block and products', then choose your newly create 'Subcategories' block from the 'CMS Block' menu. Then save.


2 Answers

If you want any number of subcategories of parent category than Click here http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html

like image 121
user3146094 Avatar answered Oct 07 '22 13:10

user3146094


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.

like image 45
liyakat Avatar answered Oct 07 '22 13:10

liyakat