0

I am attempting to fetch the sub-categories of a parent category in Magento 1.9.2. I've created a file 'subcategories.phtml' and reference this in a static block. I know the reference in the static block works as it shows the words 'test'.

However, if I update the file with the code below it shows only a blank page. Can anyone please advise an update to get this working?

<?php 
//If there are sub categories
$categories = $this->getCurrentChildCategories();
$categoriescount = $this->getCurrentChildCategories()->count();
if ($categoriescount > 0): 
?>
<div class="sub-category-container">    
    <?php 
    //Loop through categories
    foreach ($categories as $category):
    ?>
    <div class="sub-category">
        <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
        <?php 
        // If there is a thumbnail set for the category - Display it
        if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getThumbnail()):?>
        <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" width="220" height="110" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
        <?php endif; ?>
        </a>
        <div class="inner-sub-category">
            <a href="<?php echo $this->getCategoryUrl($category)?>" class="sub-link"><?php echo $category->getName()?></a>
            <a href="<?php echo $this->getCategoryUrl($category)?>" class="btn"><span>View All</span></a>
        </div>
    </div>

    <?php endforeach; ?>
</div>
<?php else:?>
<p>No Sub Categories</p>
<?php endif; ?>
  • Please let me know if you found a working solution? – sv3n Aug 28 '17 at 19:05
1

You can used recursive function to get sub category within sub category to nth level.This code might help you i create a function in which i pass category id and check whether it has sub category or not.

$rootcatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
if (Mage::getStoreConfig('customconfig_options/section_one/custom_field_nine') != '') {
    $array= '<ul>';
    foreach ($cat_name as $key=>$c) {
        $array .= '<li>'.
        $c . "<input type='radio' name='category' id='category' style='margin-left:5px' value='".$key."'>\n";
        $array .= '</li>';
    }
    $array.= '</ul>';
    echo $array;
} else {
    function get_categories($categories) {
        $array= '<ul>';
        foreach ($categories as $category) {
            $cat = Mage::getModel('catalog/category')->load($category->getId());
            $count = $cat->getProductCount();

            $array .= '<li>'.
            $category->getName() . "<input type='radio' name='category' id='category_".$category->getId()."' style='margin-left:5px' value='".$category->getId()."'>\n";
            if ($category->hasChildren()) {
                $children = Mage::getModel('catalog/category')->getCategories($category->getId());
                $array .=  get_categories($children);
            }
            $array .= '</li>';

        }
        return  $array . '</ul>';
    }
    echo  get_categories($categories);
}
0

You can try this:

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 5;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>

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

                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
0

You can get sub-category data in this way ... but therefor you have to change your HTML a bit. You may inspect $cat->getData() to see all available data.

From How should i get image url in my category tree?

$storeId = 3;
$recursionLevel = 3;

$parent = Mage::app()->getStore($storeId)->getRootCategoryId();
$tree = Mage::getResourceModel('catalog/category_tree');
/* @var $tree Mage_Catalog_Model_Resource_Category_Tree */

$nodes = $tree->loadNode($parent)
    ->loadChildren($recursionLevel)
    ->getChildren();
$collection = $tree->getCollection()
    ->addAttributeToSelect('*');
$tree->addCollectionData($collection, false, $parent);

foreach ($collection as $cat) {
    # inspect possible data and adjust your html
    var_dump($cat->getData());
    break;
}

Another way to get all to data for categories is this ... How to write function for getting subcategories of particular category?

public function getCategories()
{
    $categoryId = 2;
    $categoryTree = Mage::getResourceModel('catalog/category')->getCategories($categoryId);

    $array = array();
    foreach ($categoryTree as $category) {
        $array[$category->getPathId()] = $category->getName();
        $array = array_merge($array, $this->_getChildren($category));
    }
}

public function _getChildren($category, $array = array())
{
    if ($category->getChildrenCount()) {
        foreach ($category->getChildren() as $child) {
            $array[$child->getPathId()] = $child->getName();
            $this->_getChildren($child, $array);
        }
    }
    return $array;
}
0

In your subcategories.phtml , paste the below code.

<?php 
  $layer = Mage::getSingleton('catalog/layer');
  $_category = $layer->getCurrentCategory();
  $currentCategoryId= $_category->getId();
  $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
?>
<?php foreach ($children as $category): ?>
      <a href="<?php echo $children->getUrl() ?>">
        <img src="<?php echo $children->getImageUrl(); ?>" />
      </a>
<?php endforeach; ?>
  • Not working for me in 1.9.2 – YorkieMagento Jul 31 '17 at 17:55
  • In which page , u want to fetch the subcategory ?? – abhishek Aug 1 '17 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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