Magento:如何通过观察者添加句柄
我试图使用Magento:如何通过观察者添加句柄,magento,magento-1.7,Magento,Magento 1.7,我试图使用addHandle(),但使用以下命令会产生错误: public function HandleMe($observer) $update = $observer->getEvent()->getLayout()->getUpdate(); $update->addHandle('handlename'); 引发一个“致命错误:调用成员函数getUpdate()”您必须在更新布局之前加载核心/布局,因此请尝试执行以下代码 public funct
addHandle()
,但使用以下命令会产生错误:
public function HandleMe($observer)
$update = $observer->getEvent()->getLayout()->getUpdate();
$update->addHandle('handlename');
引发一个“致命错误:调用成员函数
getUpdate()
”您必须在更新布局之前加载核心/布局,因此请尝试执行以下代码
public function addCustomHandles($observer) {
$update = Mage::getSingleton('core/layout')->getUpdate();
//Your code here..
}
或参考下面的链接
尝试使用以下方法: 首先将其添加到CustomModule中的config.xml中:
<config>
<frontend>
<events>
<controller_action_layout_load_before>
<observers>
<yourcustomtheme_observer>
<class>yourcustomtheme/observer</class>
<method>addHandles</method>
</yourcustomtheme_observer>
</observers>
</controller_action_layout_load_before>
</events>
</frontend>
</config>
PS:这仅供参考,以便您可以检查您是否正确使用了观察者和句柄。您正在观察的事件是什么?或者客户注册成功,基本上我想运行一个脚本或包含一个js文件,一旦新客户注册,
布局
对象不会传递给事件。像埃拉瓦拉桑说的那样试试看。但我有一种感觉,这些活动都不起作用。事件customer\u save\u After
和customer\u register\u success
发出后,将进行重定向。在出现的新页面中,布局句柄将不再可用。您可以尝试使用这些事件在会话中设置值,并在下一页检查该值是否已设置,然后在其他事件中添加句柄。像controller\u action\u layout\u generate\u xml\u在
之前。在这里,您可以访问布局对象。谢谢您的回答。我尝试使用Mage::getSingleton('core/layout')->getUpdate(),然后使用addHandle('handlename')并在my local.xml中定义了句柄,但它没有显示它。我做错什么了吗?。再次感谢,就像我在之前的评论中所说的,很可能它不会工作,因为在发送事件后,页面会刷新。在新页面中,事件不再发送。感谢您的回答。我尝试使用Mage::getSingleton('core/layout')->getUpdate(),然后使用addHandle('handlename')并在my local.xml中定义了句柄,但它没有显示它。再次感谢您收到相同的错误..?并尽量避免使用local.xml,请始终尝试将您的配置放在config.xml文件中。您能给我看一下您的配置(local.xml)和布局句柄更新吗。。?。在observer.php中,它是:公共函数handle_me($observer){$update=$observer->getEvent()->getLayout()->getUpdate();$update->addHandle('custom_layout_handle');返回$this;}在local.xml中是file.js,observer会触发,非常感谢。什么是“CLS”你在上面的课程中提到的…?正如我所说的,这是供参考的,这样你就可以正确地创建一个观察者,CLS什么都不是,我已经在我的安装中将法师重命名为CLS。。。您将其替换为Mage_Core_Model_Abstract。。希望这是好的。
class YourPackage_YourCustomTheme_Model_Observer extends CLS_Core_Model_Abstract
{
public function addHandles($observer) {
$category = Mage::registry('current_category');
if ($category instanceof Mage_Catalog_Model_Category) {
$update = Mage::getSingleton('core/layout')->getUpdate();
$fertilility = (count($category->getChildrenCategories()->getData())) ? 'parent' : 'nochildren';
$update->addHandle('catalog_category_' . $fertilility);
}
return $this;
}
}