0
votes

I am trying to add the sub category name for each item in the list or grid view. The veiw that I am trying to add this info to is the parent category which has no products in it except for the children category products.

This is where I feel NEW to Magento... which I am, this should be totally easy, and it probably is... But how!

I can get the category like this...

$category = Mage::getModel('catalog/layer')->getCurrentCategory();

...and I can then echo it using this...

echo $category->getName();

and I have tried to get the sub category by doing this...

$subcategory = $category->getChildrenCategories();

but when I echo the $subcategory...

echo $subcategory->getName();

...it is error city!

Could someone tell me where I am flawed, this should be as easy as it gets. I would think that it should be as easy as $this-getCategory()-getName(); or something like that.

Anyways, if someone can help me get a grip on this I would be very appreciative. Thanks!

2

2 Answers

0
votes

Here is the code to get subcategories data such as name

$category = Mage::getModel('catalog/layer')->getCurrentCategory();
$subcategory = $category->getChildrenCategories()->getData();
foreach($subcategory as $sub) {
    $subid = $sub['entity_id'];
    $catModel = Mage::getModel('catalog/category')->load($subid);
    $subcat_name = $catModel['name']; //sub category name
    echo $subcat_name;
}
0
votes

Hey Can you please try it with code:-

    <?php
$_helper = Mage::helper('catalog/category') ?>
    <?php
$_categories = $_helper->getStoreCategories() ?>
    <?php
$currentCategory = Mage::registry('current_category') ?>
    <?php

if (count($_categories) > 0): ?>
      <?php
foreach($_categories as $_category): ?>
    <a href="<?php
echo $_helper->getCategoryUrl($_category) ?>">
       <?php
echo $_category->getName() ?>
     </a>
       <?php
$_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
         <?php
$_subcategories = $_category->getChildrenCategories() ?>
          <?php
if (count($_subcategories) > 0): ?>
           <?php
foreach($_subcategories as $_subcategory): ?>
      <a href="<?php
echo $_helper->getCategoryUrl($_subcategory) ?>">
             <?php
echo $_subcategory->getName() ?>
                 </a>
  <?php
endforeach; ?>

    <?php
endif; ?>

            <?php
endforeach; ?>

    <?php
endif; ?>