Question

I have a category list page which uses the 1column.phtml template. However, when the user navigates to a category where there are no subcategories - and only its products are displayed - I would like to call the left column. The problem is that it is that I am still using the 1column template for this situation.

How can I call the left column?

Was it helpful?

Solution

If I remember correctly, the CategoryController adds a layout handle for categories with no subcategories [link], meaning that this can be done via layout XML, e.g. in local.xml layout file:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <catalog_category_layered_nochildren>
        <action method="setTemplate" block="root">
            <tpl>page/2columns-left.phtml</tpl>
        </action>
    </catalog_category_layered_nochildren>
</layout>

OTHER TIPS

For a single category:

You can change the layout for the single category. To do this, go to the categorie in the backend, open the tab Custom Layout and choose "2 columns with left bar" in Page Layout dropdown.

Implementing an observer

I think listening on core_block_abstract_to_html_before might be a good idea. Check wether the block is root. If it is a category, the category can be found in Mage::registry('current_category');. Then you check for hasChildren() and if it has, set the Template for the root block.

I'm not sure whether this is enough. Maybe you need to change the update handle too.

Implementation

Observer.php

public function coreBlockAbstractToHtmlBefore(Varien_Event_Observer $observer)
{
    /* @var $block Mage_Core_Block_Template */
    $block = $observer->getBlock();
    if($block->getNameInLayout() == 'root') {
        /* lvar $category Mage_Catalog_Model_Category */
        if(($category = Mage::registry('current_category')) && $category instanceof Mage_Catalog_Model_Category) {
            if(!$category->hasChildren()) {
                $block->setTemplate('page/2columns-left.phtml');
            }
        }
    }
}

config.xml

<config>
    <frontend>
        <events>
            <core_block_abstract_to_html_before>
                <observers>
                    <namespace_mymodule>
                        <type>model</type>
                        <class>Namespace_MyModule_Model_Observer</class>
                        <method>coreBlockAbstractToHtmlBefore</method>
                    </namespace_mymodule>
                </observers>
            </core_block_abstract_to_html_before>
        </events>
    </frontend>
</config>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top