4

I found an observer and modified it a bit which should overwrite the standard product layout. This works, however, if I have cache enabled it keeps loading my page from cache.

What I actually need is, that as soon as the GET parameter "template" is set the page should be cached so I don't have any problems.

Normal product ie: http://www.website.com/product.html Product in popup: http://www.website.com/product.html?template=popup

With cached disabled it works, enabled it doesn't. Hope somebody can help me.

Here's the 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 != '')
            {

                $cache = Mage::app()->getCacheInstance();
                $cache->banUse('full_page');

                $update = $layout->getUpdate();
                $update->load($template);
                $layout->generateXml();
            }
        } 
    }
}

And the XML

<?xml version="1.0"?>
<layout version="0.1.0">
  <default></default>
  <popup translate="label">
    <label>Catalog Product View Popup</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="catalog.product.related"/>
      <remove name="product.attributes"/>
      <remove name="product.info.upsell"/>
      <remove name="product.description"/>
        <remove name="product.info.additional"/>
        <remove name="product.description"/>
        <remove name="product.info.container1"/>
        <remove name="product.info.container2"/>
    </reference>
  </popup>
</layout>
3
  • Which event are you observing?
    – benmarks
    Aug 30, 2013 at 18:05
  • Also, unsetChildren() might be a cleaner choice for your content updates.
    – benmarks
    Aug 30, 2013 at 18:06
  • I've updated the code with the unsetchildren. When the productpage loads it must detect if the get param "template" is loaded. If so, then this needs to override the page layout completelly. Now it only loads properly if cache is turned off. Aug 31, 2013 at 6:41

1 Answer 1

1

So the code example below was working for me with the event "controller_action_layout_load_before" as I could not see this noted in your question. It would work with and without cache enabled and on both rendering the regular template and the new popup template. Please updated me if this is not the correct event or if this is not one that you can use and I will try changing my code.

/app/etc/modules/Manners_Test.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Manners_Test>
            <active>true</active>
            <codePool>local</codePool>
        </Manners_Test>
    </modules>
</config>

/app/code/local/Manners/Test/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Manners_Test>
            <version>0.0.1</version>
        </Manners_Test>
    </modules>
    <global>
        <models>
            <manners_test>
                <class>Manners_Test_Model</class>
            </manners_test>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <manners_test>
                        <class>manners_test/observer</class>
                        <method>changeLayoutEvent</method>
                    </manners_test>
                </observers>
            </controller_action_layout_load_before>
        </events>
        <layout>
            <updates>
                <manners_test>
                    <file>manners_test.xml</file>
                </manners_test>
            </updates>
        </layout>
    </frontend>
</config>

/app/design/frontend/base/default/layout/manners_test.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <default></default>
    <popup translate="label">
        <label>Catalog Product View Popup</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="catalog.product.related"/>
            <remove name="product.attributes"/>
            <remove name="product.info.upsell"/>
            <remove name="product.description"/>
            <remove name="product.info.additional"/>
            <remove name="product.description"/>
            <remove name="product.info.container1"/>
            <remove name="product.info.container2"/>
        </reference>
    </popup>
</layout>

/app/code/local/Manners/Test/Model/Observer.php

<?php
class Manners_Test_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 != '') {
                $cache = Mage::app()->getCacheInstance();
                $cache->banUse('full_page');

                $update = $layout->getUpdate();
                $update->load($template);
                $layout->generateXml();
            }
        }
    }
}

Your Answer

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

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