如何“强迫” Magento加载不同的布局
背景:我需要能够在具有添加到购物车功能的灯箱中加载追加销售/交叉销售产品。
我实现这一目标的想法是“强制”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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法让它完全按照我最初的预期工作。感谢@Jonathan Day 让我意识到它不起作用的原因是微不足道的。
Config.xml:
Observer.php:
Local.xml:
.phtml 文件中的产品 url:
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:
Observer.php:
Local.xml:
Product url in .phtml file:
为什么不想只使用常规布局更新?
如果您想根据某些条件更改产品页面的设计,您可以使用布局处理程序功能。这意味着您必须检查控制器中的参数并添加布局更新的处理程序,然后您可以像任何其他处理程序一样在布局文件中使用它。例如:
在布局中:
检查详细信息
Mage_Catalog_ProductController::_initProductLayout()
Why don't you want to use just regular layout udates?
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:
And in layout:
Check for details
Mage_Catalog_ProductController::_initProductLayout()