Magento 解除观察者阻止

Magento unsetting block by observer

我有一个观察者:

public function removePrice($observer) {
    // $observer contains data passed from when the event was triggered.
    // You can use this data to manipulate the order data before it's saved.
    // Uncomment the line below to log what is contained here:
    $layout = $observer->getEvent()->getLayout();
    if ($block = $layout->getBlock('catalog_product_price_template')) {
        $layout->unsetBlock('catalog_product_price_template');
    }
}

和我的 config.xml 我的观察员:

...
<controller_action_layout_generate_blocks_after>
    <observers>
        <...>
            <type>singleton</type>
            <class>...</class>
            <method>removePrice</method>
        </...>
    </observers>
</controller_action_layout_generate_blocks_after>
....

在我的 catalog.xml 我的商店 app\design\frontend\default\myShop\layout\catalog 中定义了块:

<default>
    <block type="catalog/product_price_template" name="catalog_product_price_template" />
</default>

Observer 调用正确。问题是块 catalog_product_price_template 仍然出现在前端。也许我这里有错误的事件?

很高兴得到任何答案!:)。

您可以使用更简单的解决方案。将删除元素添加到您的 XML。

public function removePrice($observer) {
    $layout = $observer->getLayout();
    $layout->getUpdate()->addUpdate('<remove name="catalog_product_price_template"/>');              
    $layout->generateXml();
}

另一个问题可能是您的 XML 块名称不正确。尝试移除块并查看块是否被移除。如果它不是正确的块,则首先搜索该块。

并检查我们的 list.phtml 模板。也许有您手动添加的价格。