Question

I want to show different theme depends on customer group.Is there any way to set different theme dynamically?

For Eg.: Theme A for General customer group and Theme B for Wholesale customer group.

Thanks in advance

Was it helpful?

Solution

The quick and ugly way is to check the current customer's group and then set the theme programatically:

Mage::getDesign()->setArea('frontend')
    ->setPackageName('your_package')
    ->setTheme('your_theme');

But this solution definitely misses some flexibility.

The more sophisticated way would be to create a customer group layout handels and then set custom themes there. This solution is inspired by this article by Atwix.

So first you have to observe controller_action_layout_load_before event:

<events>
    <controller_action_layout_load_before>
        <observers>
            <customer_group_handle>
                <class>module/observer</class>
                <method>addCustomerGroupHandle</method>
            </customer_group_handle>
        </observers>
    </controller_action_layout_load_before>
</events>

Then in your observer class implement addCustomerGroupHandle method:

public function addCustomerGroupHandle(Varien_Event_Observer $observer)
{
    if (Mage::helper('customer')->isLoggedIn()) {
        /** @var $update Mage_Core_Model_Layout_Update */
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $groupId = Mage::helper('customer')->getCustomer()->getGroupId();
        $groupName = Mage::getModel('customer/group')->load($groupId)->getCode();
        $update->addHandle('customer_group_' . str_replace(' ', '_', strtolower($groupName)));
    }

    return $this;
}

Note: The str_replace here is not foolproof so I suggest replacing it with a regular expression which will replace all non-alphanumeric characters with underscores and then trim leading and trailing underscores.

So now you can set a custom theme for any customer group through xml:

<?xml version="1.0" encoding="UTF-8"?>
<layout>
    <customer_group_wholesale>
        <reference name=”root”>
            <action method=”setTheme”><theme>modern</theme></action>
        </reference>
    </customer_group_wholesale>
</layout>

OTHER TIPS

This is more of an addition to Tim's answer. You can create config sections for each customer group and set the values for the themes in there. This way you don't have to hard code the customer group names and you don't have to change the code each time you add a new group.
Here is an example of how you can add dynamic config fields. It involves creating a new renderer for a config section group.
Combining in an observer this with what Tim suggested :

Mage::getDesign()->setArea('frontend')
    ->setPackageName('your_package')
    ->setTheme('your_theme');

should get your desired result.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top