2015-09-25 99 views
1

我正在使用現有的magento頁面。登錄的用戶可以更改他的個人資料信息(名字,姓氏,電子郵件等),他們可以更改他們的帳單和送貨地址。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地址並相應地發送通知。這工作正常,直到用戶選中「設爲默認送貨地址」複選框。在這種情況下,我突然收到5封電子郵件:

  • 帳單地址已變更
  • 送貨地址變更
  • 送貨地址變更
  • 帳單地址已變更
  • 客戶信息變更

所以,這些事件突然被多次觸發,並且customer_address_save_after以某種方式觸發了customer_save_after事件。有沒有辦法來防止這個或檢查哪個事件觸發了另一個事件或類似的事情?或者還有其他方法來處理這個問題嗎?

我非常感謝任何提示,非常感謝。

+0

您正在收到5封郵件,可能是因爲 - customer_save_after事件每當客戶更改任何數據(包括地址,基本配置文件信息等)時都會觸發。而您的第二個customer_address_save_after事件會特別觸發地址更改。 – Ranjana

+0

但如果客戶編輯地址,我只收到一封電子郵件。只有當複選框被選中時纔會觸發customer_save_event – Chi

+1

沒關係,所以可能這與您的問題有關,可以幫助您http://stackoverflow.com/questions/5838346/magento-customer-save-after-always-fired-兩次 – Ranjana

回答

0

我真的不能解決問題,與mage_registry我alawys有一個錯誤,所以我決定去一個完全不同的方法。

在我的擴展中,我刪除了觀察者,而是創建了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。這工作非常好。

感謝您的幫助,每個人。