如何“强制”Magento加载不同的布局(How to 'force' Magento to load a different layout)

编程入门 行业动态 更新时间:2024-10-25 19:22:07
如何“强制”Magento加载不同的布局(How to 'force' Magento to load a different layout)

背景:我需要能够在装有购物车功能的灯箱中加载upsell / crosssell产品。

我的想法是“强制”Magento以不同的布局加载产品。 我想在controller_action_layout_generate_xml_before事件(下面的代码)上使用观察者。

不幸的是,我有没有工作。 任何指针(或完全不同/更好的想法)非常感谢。

<?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(); } } }

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(); } } }

最满意答案

我设法完全按照我第一次打算的那样工作。 感谢@Jonathan Day让我意识到它不工作的原因是微不足道的。

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>

.phtml文件中的产品网址:

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

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';

更多推荐

本文发布于:2023-08-03 19:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395726.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布局   加载   Magento   layout   load

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!