0

I want 1 column layout for main category and for sub-category 2 column layout, basically want to remove left side section for main category

In my phtml file code like below, want to change layout from phtml file.

$_category  = $this->getCurrentCategory();
$category = Mage::getModel('catalog/category')->load($_category->getId());
$subCategories = $category->getChildrenCategories();

if(count($subCategories) > 0) {
<action method="setTemplate"><template>onecolumns.phtml</template></action>
}else{
<action method="setTemplate"><template>2columns.phtml</template></action>
}
-1

Method 1: Custom design

You can use custom layout update from the admin panel where you edit the category.

It is under "custom design"

enter image description here enter image description here

Method 2: Overrides

So you need to override the CategoryController which should be under \app\code\core\Mage\Catalog\controllers\CategoryController.php

Some example I was able to google out about how to override a controller:

Overriding controller

Now you need to take a look on the viewAction() in the CategoryController

/**
     * Category view action
     */
    public function viewAction()
    {
        if ($category = $this->_initCatagory()) {
            $design = Mage::getSingleton('catalog/design');
            $settings = $design->getDesignSettings($category);

            // apply custom design
            if ($settings->getCustomDesign()) {
                $design->applyCustomDesign($settings->getCustomDesign());
            }

            Mage::getSingleton('catalog/session')->setLastViewedCategoryId($category->getId());

            $update = $this->getLayout()->getUpdate();
            $update->addHandle('default');

            if (!$category->hasChildren()) {
                $update->addHandle('catalog_category_layered_nochildren');
            }

            $this->addActionLayoutHandles();
            $update->addHandle($category->getLayoutUpdateHandle());
            $update->addHandle('CATEGORY_' . $category->getId());
            $this->loadLayoutUpdates();

            // apply custom layout update once layout is loaded
            if ($layoutUpdates = $settings->getLayoutUpdates()) {
                if (is_array($layoutUpdates)) {
                    foreach($layoutUpdates as $layoutUpdate) {
                        $update->addUpdate($layoutUpdate);
                    }
                }
            }

            $this->generateLayoutXml()->generateLayoutBlocks();
            // apply custom layout (page) template once the blocks are generated
            if ($settings->getPageLayout()) {
                $this->getLayout()->helper('page/layout')->applyTemplate($settings->getPageLayout());
            }

            if ($root = $this->getLayout()->getBlock('root')) {
                $root->addBodyClass('categorypath-' . $category->getUrlPath())
                    ->addBodyClass('category-' . $category->getUrlKey());
            }

            $this->_initLayoutMessages('catalog/session');
            $this->_initLayoutMessages('checkout/session');
            $this->renderLayout();
        }
        elseif (!$this->getResponse()->isRedirect()) {
            $this->_forward('noRoute');
        }
    }

Now you should replace the logic with your own adding your layout handles depending on the results.

You can access the current category within the function :

$category = $this->_initCatagory()
  • than for every category it will follow that layout, want to differentiate two main category and sub-category – Mandip Darji Aug 6 '18 at 10:05
  • Why would it ? It will affect only the category you will specify, so you specify default layout 2 columns for all categories, and update the configuration for the lvl1 categories with the custom design – Gosu Przmak Aug 6 '18 at 10:08
  • than i have to set for every category Custom Layout Update ? – Mandip Darji Aug 6 '18 at 10:10
  • Well that's one way ofc you can edit or override the block/controller and add some custom logic there. – Gosu Przmak Aug 6 '18 at 10:13
  • Well that's one way ofc you can edit or override the block/controller and add some custom logic there. – Gosu Przmak How ?????? – Mandip Darji Aug 6 '18 at 10:20

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.