magento的事件機制就跟dural的hook機制一樣,為了更好的擴充magento。magento裡面的很多操作都是有事件派送的,比如産品的儲存,删除,訂單的儲存,等等,很多很多。
事件派送很簡單,如以下代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
mage::dispatchevent('checkout_onepage_controller_success_action', array('quote_item' => $item));
可以使用 mage::addobserver來監聽事件,但一般不那樣做,基本上都是在etc/config.xml隻添加的。 如以下代碼
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<models>
<boomorder>
<class>boom_order_model</class>
</boomorder>
</models>
</global>
<frontend>
<events>
<checkout_onepage_controller_success_action>
<observers>
<boom_order_observer>
<type>singleton</type>
<class>boom_order_model_observer</class>
<method>myorderprocess</method>
</boom_order_observer>
</observers>
</checkout_onepage_controller_success_action>
</events>
</frontend>
</config>
當checkout_onepage_controller_success_action事件派送之後,會有函數會執行,它們分别是boom/order/model/ observer.php檔案裡面的myorderprocess方法。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5ichR3cf52bjl2LcNXZnFWbp9CXt92YuUWelRXauYjM5cmbvlXZkVHavw1LcpDc0RHaiojIsJye.png)
<?php
class boom_order_model_observer
{
/**
* event hook: checkout_onepage_controller_success_action
* @param $observer varien_event_observer
*/
public function myorderprocess($observer)
{
mage::log("enter myorderprocess");
$order = new mage_sales_model_order();
$incrementid = mage::getsingleton('checkout/session')->getlastrealorderid();
$order->loadbyincrementid($incrementid);
mage::log("exit myorderprocess");
}
}
sdfs