Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to 'force' Magento to load a different layout

Tags:

layout

magento

Background: I need to be able to load upsell / crosssell products in a lightbox complete with add-to-cart functionality.

My idea for achieving this was to 'force' Magento to load products in a different layout. I thought of using an observer on the controller_action_layout_generate_xml_before event (code below).

Unfortunately what I have is not working. Any pointers (or completely different / better ideas) are much appreciated.

<?php 
class My_ForceLayout_Model_Observer
{
    public function changeLayoutEvent($observer)
    {
        $action = $observer->getEvent()->getAction();
        $layout = $observer->getEvent()->getLayout();

        if($action->getRequest()->getControllerName() == 'product'
            && $action->getRequest()->getActionName() == 'view') 
        {

            $update = $layout->getUpdate();
            $update->load('popup'); // for testing only
            $layout->generateXml();
        } 
    }
}
like image 672
Toby Hemmerling Avatar asked Nov 19 '25 00:11

Toby Hemmerling


2 Answers

I managed to get this to work exactly as I first intended. Thanks to @Jonathan Day for making me realize the reason it was not working was trivial.

Config.xml:

<config>
    ....
<frontend>
    <events>
        <controller_action_layout_generate_blocks_before>
            <observers>
                <forcelayout>
                    <type>singleton</type>
                    <class>forcelayout/observer</class>
                    <method>changeLayoutEvent</method>
                </forcelayout>
            </observers>
        </controller_action_layout_generate_blocks_before>
    </events>
</frontend>
    ....
</config>

Observer.php:

class Unleaded_ForceLayout_Model_Observer
{
    public function changeLayoutEvent($observer)
    {
        $action = $observer->getEvent()->getAction();
        $layout = $observer->getEvent()->getLayout();

        if($action->getRequest()->getControllerName() == 'product'
          && $action->getRequest()->getActionName() == 'view')
        {
            $template = $action->getRequest()->template;
            if (isset($template) && $template != '')
            {
                $update = $layout->getUpdate();
                $update->load($template);
                $layout->generateXml();
            }
        } 
    }
}

Local.xml:

<popup translate="label">
    <label>Catalog Product View Lightbox</label>
    <remove name="right"/>
    <remove name="left"/>
    <reference name="root">
        <action method="setTemplate">
            <template>page/popup.phtml</template>
        </action>
    </reference>
    <reference name="content">
        <remove name="product.info.upsell"/>
    </reference>
</popup>

Product url in .phtml file:

echo $this->getProductUrl($_item) . '?template=popup';
like image 200
Toby Hemmerling Avatar answered Nov 21 '25 08:11

Toby Hemmerling


Why don't you want to use just regular layout udates?

<catalog_product_view translate="label">
    <label>Catalog Product View (Any)</label>
    <!-- Mage_Catalog -->
    <remove name="right"/>
    <remove name="left"/>
    <reference name="content">
        <block type="new_catalog/product_view" 
            name="new.product.info" 
            template="new/catalog/product/view_popup.phtml">
            ...
        </block>
    </reference>
</catalog_product_view> 

If you want to change the design of your product page depends on some conditions, you could use layout handler functionality. It means that you have to check your parameters in controller and add handler for layout updates, then you could use it in layout file as any other handler. For example:

if ($this->check_parameters()) {
    $update->addHandle('new_magic_handler');
    $this->loadLayoutUpdates();
}

And in layout:

<new_magic_handler translate="label">
    <label>New Magic</label>
    ...
</new_magic_handler> 

Check for details Mage_Catalog_ProductController::_initProductLayout()

like image 40
Eugene Tulika Avatar answered Nov 21 '25 09:11

Eugene Tulika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!