0

I am trying to call all subcategories of the current category and grab their subcategories (3rd level).

I am using the following code to call all children of the current category:

<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
        ->addAttributeToSelect(array('name'))
        ->addAttributeToFilter('is_active', 1)
        ->addIdFilter($category->getChildren())
        ->setOrder('name', 'asc')
?>

    <?php foreach ($categories as $category): ?>
    <div>
          <a href="<?php echo $category->getUrl() ?>"><img class="" src="" alt="<?php echo $this->htmlEscape($category->getName()) ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /></a>
          <div>
          <a href="<?php echo $category->getUrl() ?>"><h3><?php echo $category->getName() ?></h3></a>
          <hr>
          <a href=""><p></p></a> // Want 3rd level categories listed here
          </div>
    </div>
    <?php endforeach; ?>

This gives me a nice list of all the subcagetories of the current category but how would I insert the 3rd level categories inside this part?

          <a href=""><p></p></a>

Trying to accomplish a list like this:

Clothing
   Shirts
   Shoes
Accessories
   Scarfs
   Jewelry

2 Answers 2

0

Try this code:

<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('name', 'asc')
?>

<?php foreach ($categories as $category): ?>
<div>
  <a href="<?php echo $category->getUrl() ?>"><img class="" src="" alt="<?php echo $this->htmlEscape($category->getName()) ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" /></a>
  <div>
  <?php if($category->getLevel()==3):?>
    <a href="<?php echo $category->getUrl() ?>"><p><?php echo $category->getName() ?></p></a> // Want 3rd level categories listed here
  <?php else:?>
    <a href="<?php echo $category->getUrl() ?>"><h3><?php echo $category->getName() ?></h3></a>

  <?php endif;?>
  </div>
</div>
<?php endforeach; ?>
3
  • Thanks for the suggestion. It shows the subcategory of the current category. So it looks like this: Clothing Clothing
    – André
    Jun 16, 2016 at 8:15
  • updated ans pls check now
    – Abdul
    Jun 16, 2016 at 9:07
  • Still not showing 3rd level categories, unfortunately. I will soldier on with this conundrum until I find a working solution :-)
    – André
    Jun 16, 2016 at 9:42
0

I ended up doing something else but got the result I needed.

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 4;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>
<div>
    <div>
        <?php foreach($_categories as $_category): ?>
           <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
            <div>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><img class=""  src="" /></a>
                <div>
                    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><h3><?php echo $_category->getName() ?></h3></a>
                <hr>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php if($_category->hasChildren()):?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php foreach($_subcategories as $_subcategory): ?>
                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><p><?php echo $_subcategory->getName() ?></p></a>
                <?php endforeach; ?>
                <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.