Conditionally add blocks in the layout

Is there a way to conditionally add a block in my mafit layout, based on whether the current client is part of a group or not?

or will it be something better in the controller?

+5
source share
2 answers

It would be nice to use something like customer_logged_inand customer_logged_out, but, unfortunately, this does not exist yet ....

Copy the same method. First you need to create a module with this in config:

<frontend>
    <events>
        <controller_action_layout_load_before>
            <observers>
                <customer_group_observer>
                    <class>CUSTOM_MODULE/observer</class>
                    <method>beforeLoadLayout</method>
                </customer_group_observer>
            </observers>
        </controller_action_layout_load_before>
    </events>
</frontend>

In the class, CUSTOM_MODULE_Model_Observeradd this method:

public function beforeLoadLayout($observer)
{
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    $group = Mage::getModel('customer/group')->load($groupId);

    $observer->getEvent()->getLayout()->getUpdate()
       ->addHandle('customer_group_'.$group->getCode());
}

You can now use client groups in layout files.

<layout>
    <customer_group_General>
        <reference name="content">
            <!-- Add some blocks -->
        </reference>
    </customer_group_General>
</layout>

, , . , , .

<layout>
    <catalog_product_view>
        <reference name="content">
            <block type="core/text_list" name="group_container" />
        </reference>
    </catalog_product_view>

    <customer_group_General>
        <reference name="group_container">
            <!-- Add some blocks -->
        </reference>
    </customer_group_General>
</layout>
+16

: http://www.magentocommerce.com/boards/viewthread/83244/#t219147

customer_logged_in customer_logged_out , .

, Magento .

, , . Magento: 1.6 +

0

All Articles