Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check category have child category or not in magento

I have category id .I got the id from this code

<?php echo $current_catid=$this->getCategoryId(); ?> 

now i want to check that this category have child category or not .

if it have child than it will show the child category image and name and url.

like image 913
Vishal Sharma Avatar asked Mar 29 '13 12:03

Vishal Sharma


People also ask

How do I get sub categories in Magento 2?

Using a given code snippet, you can retrieve all the subcategories of the parent without depends on the level type. Call Block Class methods in the template, $parentID = 11; //MEN Category Id $getCategoryList = $this->getSubCategoryByParentID($parentID); <? php if (count($getCategoryList)) { ?>

What is Anchor category in Magento?

Magento 2 anchor category is the definition of the category in which the Magento native Layered Navigation can be displayed. If you need to set it as the anchor category then you need to go to Product > Categories > Display settings and switch the Anchor option to Yes. After that, don't forget to save the changes.


2 Answers

If You have current_category id then load category

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

and check count($category->getChildren());

Other methods are for count children

count($category->getChildrenNodes()); 

$category->getChildrenCount();

This way you can check if category has children or not.

getChildren() methods give you children category id and based on id you can get category image and category name.

like image 105
Mufaddal Avatar answered Oct 16 '22 07:10

Mufaddal


Please try this one, its working fine at my end

<?php  
$parentCategoryId = 10;
$categories = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArray = explode(',', $categories);
foreach($catArray as $child)
{
$_child = Mage::getModel( 'catalog/category' )->load( $child );
echo $_child->getName() . '<br />';
echo $_child->getUrl() . '<br />';
echo $_child->getDescription() . '<br />';
}
?>
like image 44
Manu Purohit Avatar answered Oct 16 '22 05:10

Manu Purohit