5

I need to change default layout file (1column.phtml, 2columns-left.phtml, etc.) based on currently logged in user (user group) and if he's logged in or not.

Is changing the default template possible from inside the PHP code, I mean just layout, I know how to do PHP logic?

4 Answers 4

3

Another way to this create different handler for each customer group using event controller_action_layout_load_before then on layout xml set it template.

Config.xml code is:

 <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <add_Customer_handler>
                        <class>MymoduleModelPrefix/observer</class>
                        <method>addHandler</method>
                    </add_Customer_handler>
                </observers>
            </controller_action_layout_load_before>
        </events>
  <frontend>

and Observer code is

<?php
class MymoduleNameSpace_Mymodule_Model_Observer{
 public function addHandler($observer)
    {
        $loggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
    if($loggedIn):
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    //Get customer Group name
      $group = Mage::getModel('customer/group')->load($groupId);
      //echo $group->getCode();
        $observer->getEvent()->getLayout()->getUpdate()
             ->addHandle('customer_group_'.strtolower($group->getCode()));
    endif;
    }

}

According to observer code , it create handler like

General Group Customer :customer_group_general whole Group Customer : customer_group_wholesales etc.

Logic like: customer_group_LOweCaseOfCustomergroupName

  <customer_group_general>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
</customer_group_general>

 <customer_group_wholesales>
    <reference name="root">
        <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
    </reference>
</customer_group_wholesales>
1
  • yup only if my method does not satisfy your conditions. In such case, if you want to control more customer groups, then this would be the best solution. +1 Amit Apr 28, 2015 at 6:54
4

You can change the template (layout file) in your controller.

<?php
    public function indexAction() {
        $this->loadLayout();
        if (yourcondition){ // e.g. check user group
            $this->getLayout()->getBlock('root')->setTemplate('page/templatefile.phtml');  //changes the template
        }
        $this->renderLayout();
        return $this;
    }
?>
1
  • Thanks Alvin. This solution is to the point and technically correct. It won't work in my case as I'm using Mage_Catalog_CategoryController override but I haven't mentioned it in my question :).
    – Alan
    Apr 29, 2015 at 12:51
3

The best and recommended way is using the layout handle that is uniquely created for this purpose. They are non another than customer_logged_in and customer_logged_out.

<customer_logged_in>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
</customer_logged_in>

for logged-in-customers and

<customer_logged_out>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
</customer_logged_out>

for not-logged-in customers.

Note 1: These two layout handles will be processed very early. So there is a chance to rewrite the root template by other layout handles which will processed after these layout handles.

Note 2 : Change the template file that you needed in the above example code as per your need.

Note 2 : If you want to change the template based on the customer groups, then you should go with programmatic ways

2
  • Thanks Rajeev. That's a great answer but I need to change layout programmatically, depending on the combination of log in status and customer group.
    – Alan
    Apr 29, 2015 at 12:57
  • you can try amith's answer in that case Apr 29, 2015 at 13:03
2

Untested, but this should work:

$layout = Mage::app()->getLayout();
$roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
$role = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');

if($role == "Wholesale") {
    $layout->getUpdate()->addUpdate('<reference name="root"><action method="setTemplate"><template>page/1column.phtml</template></action></reference>');
    $layout->generateXml();
}

Reference:

1
  • 1
    Thanks for the answer and reference. It didn't work in my case as I was calling it from a controller override but may work from an observer(as the reference link indicates).
    – Alan
    Apr 29, 2015 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.