check category have child category or not in magento
Asked Answered
G

5

5

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.

Gehman answered 29/3, 2013 at 12:16 Comment(0)
P
5

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 />';
}
?>
Pammie answered 29/3, 2013 at 13:11 Comment(0)
I
8

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.

Iain answered 29/3, 2013 at 12:24 Comment(3)
i tried your code but its not working ..its always showing 1 when i count the children but its have 2 child category i put this code $current_catid=$this->getCategoryId(); $category = Mage::getModel('catalog/category')->load($current_catid); echo count($category->getChildren());Gehman
Try getChildre()->count() methods instead or you can also use count($category->getChildrenNodes()); for count category childrenIain
i tried this as you told me $category->getChildren()->count(); its giving me fatal errorGehman
P
5

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 />';
}
?>
Pammie answered 29/3, 2013 at 13:11 Comment(0)
O
1

A bit old, but I was searching for the same solution and @Mufaddal's solution didn't work. Then I found getChildrenCategories().

$_category = Mage::registry('current_category');
count($_category->getChildrenCategories());
Objectivity answered 26/2, 2015 at 15:22 Comment(0)
K
1

Actually it depends on whether or not the option "Use Flat Catalog Category" is enabled.

Therefore, the best method to check category have child category or not is:

if (Mage::helper('catalog/category_flat')->isEnabled()) {
    $childrenCount = $category->getResource()->getChildrenAmount($category);
} else {
    $childrenCount = $category->getResource()->getChildrenCount();
}

with $category I suppose you have already, as:

$category = Mage::getModel('catalog/category')->load(id);
Koressa answered 25/11, 2015 at 10:17 Comment(0)
B
0

you have another option to check category child category exist or not..

  <?php
    $currentCategoryId = Mage::registry('current_category')->getId();
    $collection = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('is_active', 1) //only active categories
        ->addAttributeToFilter('parent_id', $currentCategoryId);
    $currentCat = Mage::registry('current_category');
    $subCategories = Mage::getModel('catalog/category')->load($currentCat->getParentId())->getChildrenCategories();

    if($collection->getSize() >= 1){//there some thing....}else{

//Get Subcategory....


foreach ($subCategories as $subCategoryId ): 

                     if($subCategoryId->getIsActive())
                    {  $products = Mage::getModel('catalog/category')->load($subCategoryId->getId())
                    ->getProductCollection()
                    ->addAttributeToSelect('entity_id')
                    ->addAttributeToFilter('status', 1)
                    ->addAttributeToFilter('visibility', 4);


                        <li <?php if($subCategoryId->getId()==$currentCategoryId){?>class="active"<?php } ?>>
                            <a href="<?php echo $subCategoryId->getURL(); ?>">
                                <?php //echo $subCategoryId->getName()." (".$products->count().")"; ?>
                                <?php echo $subCategoryId->getName(); ?>
                            </a>
                        </li>
                      } endforeach;}?>

if it's help full let me know...

Thanks Ravi

Buttery answered 15/4, 2015 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.