layout - call left column block within main column (in the 1column.phtml template)

magento.stackexchange https://magento.stackexchange.com/questions/1010

  •  16-10-2019
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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>

نصائح أخرى

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>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top