Magento – getChildrenCategories only includes categories with Include in navigation set to yes

category

I want to display the child categories of a category and I want to display them in the order they're in in the admin panel. I've read a lot about it and the simplest way seems to be to use getChildrenCategories as that returns the children in the right order.

However, I've found this only returns categories with Include in navigation set to yes whereas all the child categories have this set to no in my case.

I've tracked this down to _loadNodes in flat.php. I've included the relevant code from it below and you can see the hard coded where statement in the penultimate line.

My question is simply how can I stop this? Is there a simple way?

Thanks

protected function _loadNodes($parentNode = null, $recursionLevel = 0, $storeId = 0, $onlyActive = true)
{
    $_conn = $this->_getReadAdapter();
    $startLevel = 1;
    $parentPath = '';
    if ($parentNode instanceof Mage_Catalog_Model_Category) {
        $parentPath = $parentNode->getPath();
        $startLevel = $parentNode->getLevel();
    } elseif (is_numeric($parentNode)) {
        $selectParent = $_conn->select()
            ->from($this->getMainStoreTable($storeId))
            ->where('entity_id = ?', $parentNode)
            ->where('store_id = ?', $storeId);
        $parentNode = $_conn->fetchRow($selectParent);
        if ($parentNode) {
            $parentPath = $parentNode['path'];
            $startLevel = $parentNode['level'];
        }
    }
    $select = $_conn->select()
        ->from(
            array('main_table' => $this->getMainStoreTable($storeId)),
            array('entity_id',
                new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('name')),
                new Zend_Db_Expr('main_table.' . $_conn->quoteIdentifier('path')),
                'is_active',
                'is_anchor'))

        ->where('main_table.include_in_menu = ?', '1')
        ->order('main_table.position');

Best Answer

You have two options: rewrite or observer.

Rewrite:

The rewrite DOM structure for this is a bit tricky because it is a resource model:

<global>
    <models>
        <catalog_resource>
            <rewrite>
                <flat>Your_Module_Model_Resource_Flat</flat><!-- for example -->
            </rewrite>
        </catalog_resource>
    </models>
</global>

Then in the class file for Your_Module_Model_Resource_Flat you would extend from the core flat category resource model and override this method, changing what you need to change.


Observer:

Register an observer in the frontend for the catalog_category_flat_loadnodes_before event in the frontend, which you can see is dispatched just below the code you referenced. In fact it is preceded by a note stating, "Allow extensions to modify select (e.g. add custom category attributes to select)". In your observing method, you will need to manipulate the select object which is passed in; I have untested peudo-y code here:

public function allowExcludedCategories($o)
{
    $select = $o->getSelect();
    $oWhere  = $select->getPart(Varien_Db_Select::WHERE);
    $select->setPart(Varien_Db_Select::WHERE,array_shift($oWhere));
}
Related Topic