How to make Magento load a different layout

Reference Information. I need to be able to download upsell / crosssell products in a lightbox, complete with functionality to add to the cart.

My idea for achieving this goal was to “force” Magento to download products in a different format. I was thinking about using an observer in the event controller_action_layout_generate_xml_before(code below).

Unfortunately, I do not have a job. Any pointers (or completely different / best ideas) are greatly 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();
        } 
    }
}
+3
source share
2 answers

I managed to get this to work the way I first intended. Thanks to @Jonathan Day for understanding why it didn't work, it 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>

URL- .phtml:

echo $this->getProductUrl($_item) . '?template=popup';
+7

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 ($this->check_parameters()) {
    $update->addHandle('new_magic_handler');
    $this->loadLayoutUpdates();
}

:

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

Mage_Catalog_ProductController::_initProductLayout()

+1

Source: https://habr.com/ru/post/1790851/


All Articles