Vishal Sharma
Vishal Sharma

Reputation: 1372

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.

Upvotes: 5

Views: 15511

Answers (5)

NgocDB
NgocDB

Reputation: 131

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);

Upvotes: 1

Ravi Chomal
Ravi Chomal

Reputation: 459

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

Upvotes: 0

Walker
Walker

Reputation: 153

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());

Upvotes: 1

Manu Purohit
Manu Purohit

Reputation: 104

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 />';
}
?>

Upvotes: 5

Mufaddal
Mufaddal

Reputation: 5381

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.

Upvotes: 8

Related Questions