Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getStoreCategories() returns nothing

Tags:

magento

I'm currently learning Magento and seem to have come across a problem that is familiar with a lot of people in my position, yet none of the given solutions work for me. I am looking to populate the navigation menu with all the categories found in the root category but the piece of code I have to do this does not work. Here is the excerpt of code taken which should perform such a task:

<div id="utilities">
    <?php $_menu = ''?>
    <?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php $_menu .= $this->drawItem($_category) ?>
    <?php endforeach ?>
    <?php if ($_menu): ?>
    <div class="nav-container">

        <ul id="nav">

            <?php $_anyActive = false; foreach ($this->getStoreCategories() as $_category) { $_anyActive = $_anyActive || $this->isCategoryActive($_category); } ?>
             <li class="home <?php echo !$_anyActive ? 'active' : '' ?>"><a href="<?php echo $this->getUrl('')?>"><span><?php echo $this->__('Home') ?></span></a></li> 

            <?php echo $_menu; ?>
        </ul>

    </div>
    <?php endif; ?>
</div>

Now, I've troubleshooted this quite extensively, and started by doing a var_dump() on the $this-?getStoreCategories() but this returned a NULL. I know that the statement does not get any further than that method so can conclude that the error lies in this line of code:

<?php foreach ($this->getStoreCategories() as $_category): ?>

I have tried creating new root categories and populating these with subcategories (also filling with products) then changing the root category in the admin settings to this newly created one, still to no avail. I know this code is being pulled into the page as if I type static text before the offending statement it prints to the screen.

I'm flabbergasted to say the least. Any help would be so much appreciated, and thank you so much in advance!

like image 608
Liam Spencer Avatar asked Dec 22 '22 21:12

Liam Spencer


2 Answers

Your problem is that you're using a Block, that has no getStoreCategories() method. $this - is a reference to your manually created block.

Magento has high level of abstraction so every problem here can be solved in different ways.

Use:

Mage::helper('catalog/category')->getStoreCategories()

Instead of:

$this->getStoreCategories()

When it works - better create getStoreCategories() in your block and move this code to it - as supposed by Magento architecture.

like image 99
Andrey Tserkus Avatar answered Jan 07 '23 04:01

Andrey Tserkus


The recommended way to get a menu of categories is with the method Mage_Catalog_Block_Navigation::renderCategoriesMenuHtml().

So for any block that is Mage_Catalog_Block_Navigation or descends from it can use the template app/code/design/base/default/template/navigation/top.phtml which calls that method.

like image 38
clockworkgeek Avatar answered Jan 07 '23 03:01

clockworkgeek