A lot of times I’ve found the need to create a “Shop” page that shows the root category as an anchor (with layered navigation, for example). There are also a couple questions floating around on Stackexchange about how to have root categories be shown like normal categories.
By default Magento won’t show the root category for reasons I’ve still not found out.
There are a lot of modules that have that functionality, like Amasty’s Improved Navigation. However, it turns out it’s also pretty easy to do yourself.
In the Mage_Catalog_CategoryController::_initCatagory
(yes, it’s spelled wrong) there is the following check:
if (!Mage::helper('catalog/category')->canShow($category)) {
return false;
}
The canShow
method basically checks if the category exists, is active and is not a root category. If it’s a root category, it won’t allow you to show it.
All we have to do is to create a new route to a new controller that extends Mage_Catalog_CategoryController
(no need to override the route, we’re just adding a new route).
Your config.xml should contain:
<config>
...
<frontend>
<routers>
<shop_root_category>
<use>standard</use>
<args>
<module>Module_Name</module>
<frontName>shop</frontName>
</args>
</shop_root_category>
</routers>
</frontend>
</config>
And your IndexController.php:
require_once Mage::getModuleDir('controllers', 'Mage_Catalog') . DS . 'CategoryController.php';
class Vendor_MyModule_IndexController
extends Mage_Catalog_CategoryController
{
protected function _initCatagory()
{
// ....
}
public function indexAction()
{
return $this->_forward('view');
}
}
As for the initCatagory
method, copy the original parent method, and comment out the canShow
check.
In addition to that, replace the following line:
$categoryId = (int) $this->getRequest()->getParam('id', false);
... with:
$categoryId = Mage::app()->getStore()->getRootCategoryId();
That should be it to show the root category products as a normal category page.
The cool thing about this is the abstraction between layout handles and controllers, so everything will still work even though we're using a different controller. Even Lesti_Fpc will cache your custom controller out of the box because it matches with the catalog_category_view
full action name handle.