如何“强迫” Magento加载不同的布局

发布于 2024-10-16 19:31:48 字数 789 浏览 2 评论 0原文

背景:我需要能够在具有添加到购物车功能的灯箱中加载追加销售/交叉销售产品。

我实现这一目标的想法是“强制”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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-10-23 19:31:48

我设法让它完全按照我最初的预期工作。感谢@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 文件中的产品 url:

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';
凹づ凸ル 2024-10-23 19:31:48

为什么不想只使用常规布局更新?

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

Why don't you want to use just regular layout 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 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:

if ($this->check_parameters()) {
    $update->addHandle('new_magic_handler');
    $this->loadLayoutUpdates();
}

And in layout:

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

Check for details Mage_Catalog_ProductController::_initProductLayout()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文