2

Can this code (let's call this Method 1):

$categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('*');
foreach($categories as $category) {
    [...]
}

be used a drop-in replacement for this (let's call this Method 2)?

$helper = Mage::helper('catalog/category');
$categories = $helper->getStoreCategories();
foreach($categories as $category) {
    [...]
}

The new theme I installed uses Method 2 to retrieve categories with which to populate the header menu. The code seems to be failing, and the header menu is empty.

Here is the relevant code:

public function renderCategoriesMenuHtml($momenu = FALSE, $level = 0, $outermostItemClass = '', $childrenWrapClass = '')
{
    $this->_isMomenu = $momenu;
    $activeCategories = array();
    foreach ($this->getStoreCategories() as $child) {
        if ($child->getIsActive()) {
            $activeCategories[] = $child;
        }
    }
    $activeCategoriesCount = count($activeCategories);
    $hasActiveCategoriesCount = ($activeCategoriesCount > 0);

    if (!$hasActiveCategoriesCount) {
        return '';
    }

    [...]

$this is of class Novaworks_Megamenu_Block_Mainmenu_Menu which extends Mage_Catalog_Block_Navigation.

I've verified that the getStoreCategories() method exists.

I did a var_dump $this->getStoreCategories() and it returned a massive object of class Varien_Data_Tree_Node_Collection which did not contain any category names.

Any code placed inside the above foreach ($this->getStoreCategories() as $child) loop had no effect.

So because apparently getStoreCategories() returns a non-usable result, the renderCategoriesMenuHtml function returns an empty string, and the header menu is not populated.

I was able to correctly retrieve a list of categories using Method 1 shown above, which I found by Googling.

In Method 1, $categories is an object of class Mage_Catalog_Model_Resource_Category_Collection. Each $category is an object of class Mage_Catalog_Model_Category.

Is Mage::helper('catalog/category')->getStoreCategories() producing an incorrect result on my setup? Is THAT what it's actually supposed to output?

And if so, can anyone explain why Mage::helper('catalog/category')->getStoreCategories() doesn't work for me, since it kind of looks like the conventional way of retrieving categories?

Thanks.

2

the getStoreCategories method returns a collection of Mage_Catalog_Model_Category_Tree which is a collection of categories with the correct hierarchy and a limited number of attributes added like name and URL. This would be more suited for building a menu though.

As to why it doesn't work. Make sure your flat categories are on under System > Configuration > Catalog > Frontend and all data should be reindexed.

Otherwise I'd suggest you start debugging in Mage_Catalog_Model_Resource_Category::getCategories since that's where it returns some actual collections

  • Enabling flat categories solved my problem. – js.lim.87 Sep 10 '15 at 8:45
  • Disabling it basically breaks Magento. not too sure why its even an option. Glad it helped – Sander Mangel Sep 10 '15 at 8:51
  • 2
    For anyone reading, it is also worth noting that after enabling flat categories, using Method 1 resulted in all categories being displayed without hierarchy, which is not acceptable. The stock method, Method 2, produced the correct result (i.e. subcategories appear under main categories in the form of dropdown menus). So Method 1 is not a drop-in replacement for Method 2. – js.lim.87 Sep 10 '15 at 9:00

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.