背景:我需要能够在装有购物车功能的灯箱中加载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';更多推荐
发布评论