2012-11-30 64 views
1

天兒真好所有,事件代碼不工作

我試着寫我的第一個Magento的事件處理程序來捕捉「checkout_cart_save_after」,但我不能讓我的Observer.php代碼去看看網站magento事件2天后,我瞭解整個過程,但代碼沒有運行..我很困惑,爲什麼。

這裏是在/ var/Magento的/應用/ etc/modules中的模塊XML文件/

<?xml version="1.0"?> 
<config> 
    <modules> 
    <Namespace_Yourmodule> 
     <codePool>local</codePool> 
     <active>true</active> 
     <version>1.0.0</version> 
    </Namespace_Yourmodule> 
    </modules> 
</config> 

正如你可以看到我只是用命名空間Yourmodule我在寫爲別人的教程....

在我的模塊等目錄(/ var/Magento的/應用/代碼/本地/命名空間/ Yourmodule /模塊)我有config.xml中

<?xml version="1.0"?> 
<global> 
    <events> 
     <checkout_cart_save_after> 
      <observers> 
       <yourmodule_after_observer> 
        <type>singleton</type> 
        <class>Namespace_Yourmodule_Model_Observer</class> 
        <method>checkout_cart_save_after</method> 
       </yourmodule_after_observer> 
      </observers> 
     </checkout_cart_save_after> 
    </events> 
</global> 

,並在模塊的目錄,我有我Observer.php文件:

class Namespace_Yourmodule_Model_Observer 
{ 
/*---------------------------------------- 
* 
* LogInfo() 
* 
* Basic logging of activity to disk for debugging 
*/ 
     public function LogInfo($msg) 
     { 
       $logfile="/logs/Namespace-module.log"; 
       $fd=fopen($logfile,"a"); 
       if($fd) 
       { 
         fwrite($fd,$msg."\n"); 
         fclose($fd); 
       } 
     } 

    public function checkout_cart_save_before($observer) 
    { 
     LogInfo("save_before called"); 
    } 

    public function checkout_cart_save_after($observer) 
    { 
     LogInfo("save_after called"); 
    } 
} 

試圖從車中添加和刪除項目的結果是,沒有日誌文件crearted和手動創建的時候,沒有數據被寫入,system.log顯示沒有錯誤,如果我傾斜XML它報告錯誤,所以正在讀取XML文件。

任何想法,我已經錯過了?

希德

更新12/2:

我已經度過了週末,就這個問題和解決問題,謝謝你的提示,這是的,我應該拿起小事組合並且XML文件的格式現在與上面提出的內容相匹配......現在有一個教程!我完全記錄了工作代碼和配置文件的過程。

http://z900collector.wordpress.com/magento/magento-events/

+0

「以我模塊等目錄(/ var/Magento的/應用/代碼/本地/命名空間/ Yourmodule /模塊)我有config.xml「 - 你的意思是/ var/magento/app/code/local/Namespace/Yourmodule/**等等。 – benmarks

+0

我shoudl已經發現了這個,但其/模型不/​​ /模塊! – z900collector

回答

0

嘗試更新config.xml中

<?xml version="1.0"?> 
<config> 
<modules> 
    <Namespace_Yourmodule> 
     <version>1.0</version> 
    </Namespace_Yourmodule> 
</modules> 
<global> 
    <!--helpers> 
     <yourmodule> 
      <class>Namespace_Yourmodule_Helper</class> 
     </yourmodule> 
    </helpers--> 
    <models> 
     <yourmodule> 
      <class>Namespace_Yourmodule_Model</class> 
     </yourmodule> 
    </models> 
    <events> 
     <checkout_cart_save_after> 
      <observers> 
       <yourmodule_after_observer> 
        <type>singleton</type> 
        <class>Namespace_Yourmodule_Model_Observer</class> 
        <method>checkout_cart_save_after</method> 
       </yourmodule_after_observer> 
      </observers> 
     </checkout_cart_save_after> 
    </events> 
</global> 
</config> 

Customize Magento using Event/Observer

+0

我花了這個週末,並解決了這個問題,感謝提示,這是我應該拿起的小事情的組合,現在XML文件的格式與上面提出的內容相匹配......有現在是一個教程!我完全記錄了工作代碼和配置文件的過程。 http://z900collector.wordpress.com/magento/magento-events/ – z900collector

+0

完成:),謝謝。 – z900collector