The verification category has a child category or not in purple

I have a category id. I got an identifier from this code

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

Now I want to check that this category has a child category or not .

if it has a child that displays the image and the name and URL of the child category.

+5
source share
5 answers

Please try this; its work is excellent at the 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 />';
}
?>
+4
source

If you have current_category id then load the category

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

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

Other methods are for counting children.

count($category->getChildrenNodes()); 

$category->getChildrenCount();

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

getChildren() .

+8

, , @Mufaddal . getChildrenCategories().

$_category = Mage::registry('current_category');
count($_category->getChildrenCategories());
+1

Actually, it depends on whether the option "Use flat catalog category" is enabled.

Therefore, the best method for checking a category has a child category or not:

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

with category $ I assume that you are already like:

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

you have another opportunity to check the category of the category of children 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 this helps me, let me know ...

Thanks Ravi

0
source

All Articles