我正在制作一个现有的magento页面。登录的用户可以更改他的个人资料信息(名字、姓氏、电子邮件等),他们可以更改他们的账单和送货地址。
我需要做的是在客户更改其基本信息或其中一个地址时发送通知电子邮件。我为两个事件创建了一个观察者:
<frontend>
<events>
<customer_save_after>
<observers>
<ext_customer_save_after>
<type>singleton</type>
<class>ext/observer</class>
<method>customerSaveAfter</method>
</ext_customer_save_after>
</observers>
</customer_save_after>
<customer_address_save_after>
<observers>
<ext_customer_save_after>
<type>singleton</type>
<class>ext/observer</class>
<method>customerAddressSaveAfter</method>
</ext_customer_save_after>
</observers>
</customer_address_save_after>
</events>
</frontend>
在customerSaveAfter中,我发送电子邮件,在customerAddressSaveAfter中,我检查当前ID是否与defaultbillingaddress或defaultshipping address相同,并相应地发送通知。在用户勾选"设置为默认送货地址"复选框之前,这一切都很正常。在这种情况下,我突然收到5封邮件:
- 账单地址变更
- 收货地址变更
- 收货地址变更
- 账单地址变更
- 客户信息更改
因此,突然事件被触发多次,并且customer_address_save_after以某种方式触发customer_save_after事件。是否有办法防止这种情况或检查哪个事件触发了另一个事件或类似的事情?还是有其他方法来处理这个问题?
我真的很感谢任何提示,非常感谢。
我不能真正解决这个问题,使用mage_registry我总是得到一个错误,所以我决定去一个完全不同的方法。
在我的扩展中,我删除了观察者,而是创建了2个新的控制器,AccountController和AddressController,以覆盖Magentos标准控制器来处理客户和地址保存。
在我的config.xml中我添加了这个:
<frontend>
<routers>
<customer>
<args>
<modules>
<my_ext before="Mage_Customer_AccountController">My_Ext</my_ext>
<my_ext before="Mage_Customer_AddressController">My_Ext</my_ext>
</modules>
</args>
</customer>
</routers>
</frontend>
例如,我的AccountController看起来像这样:
<?php
require_once Mage::getModuleDir('controllers','Mage_Customer').DS."AccountController.php";
class My_Ext_AccountController extends Mage_Customer_AccountController{
public function editPostAction(){
//I copied the code from the Magento AccountController here and at the proper line I added my own code
}
}
AddressController也是一样。这真的很有效。
谢谢大家的帮助。