2010-06-15 134 views
4

我想通過Web服務更改遠程清單,我知道通過Event Observer方法可以激發我的代碼,但我不知道哪個事件對於完成我的任務是有用的,比如on_order_complete,是有更新的事件列表或更多文檔?Magento訂單狀態更改事件

回答

16

如果要分派事件時的順序更改任何狀態或者狀態的狀態,那麼你需要插入自己的事件偵聽器。這聽起來並不難。

簡單地覆蓋_setStatus功能Mage_Sales_Model_Order是這樣的...

/** 
* Order model 
* 
* @category WMG 
* @package  WMG_Sales 
* @author  Lee Bolding <[email protected]> 
* 
* Supported events: 
* sales_order_status_before 
* sales_order_status_after 
* 
* NOTE: Unfortunately, we can't override setState() as the protected _setState() 
* function is used by the registerCancellation() and _checkState() functions 
* 
*/ 
class WMG_Sales_Model_Order extends Mage_Sales_Model_Order 
{ 
    /** 
    * Order state protected setter. 
    * By default allows to set any state. Can also update status to default or specified value 
    * Сomplete and closed states are encapsulated intentionally, see the _checkState() 
    * 
    * @param string $state 
    * @param string|bool $status 
    * @param string $comment 
    * @param bool $isCustomerNotified 
    * @param $shouldProtectState 
    * @return Mage_Sales_Model_Order 
    */ 
    protected function _setState($state, $status = false, $comment = '', $isCustomerNotified = null, $shouldProtectState = false) 
    { 
     // dispatch an event before we attempt to do anything 
     Mage::dispatchEvent('sales_order_status_before', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState)); 

     // attempt to set the specified state 
     if ($shouldProtectState) { 
      if ($this->isStateProtected($state)) { 
       Mage::throwException(Mage::helper('sales')->__('The Order State "%s" must not be set manually.', $state)); 
      } 
     } 
     $this->setData('state', $state); 

     // add status history 
     if ($status) { 
      if ($status === true) { 
       $status = $this->getConfig()->getStateDefaultStatus($state); 
      } 
      $this->setStatus($status); 
      $history = $this->addStatusHistoryComment($comment, false); // no sense to set $status again 
      $history->setIsCustomerNotified($isCustomerNotified); // for backwards compatibility 
     } 

     // dispatch an event after status has changed 
     Mage::dispatchEvent('sales_order_status_after', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified, 'shouldProtectState' => $shouldProtectState)); 

     return $this; 
    } 
} 

現在,您可以訂閱觀察員新建sales_order_status_beforesales_order_status_after事件

0

使用grep來查找事件列表,它必須像

grep -rn -A2 --include="*.php" dispatchEvent /var/www/magento/ 

或類似的東西...

+1

這會顯示默認核心事件。他需要實現自己的事件,因爲它不是由Magento的提供核心。 – Strae 2012-01-26 14:51:07

1

我做了blog post這個(其中包含完整的事件列表Magento CE 1.4)幾周前。

您可能感興趣的訂單放置事件是sales_order_place_after,在訂單下達後會被調用(認真!)。

希望有幫助!

謝謝, 喬

+0

感謝Joe,出色的帖子,但我有一些問題: 這個sales_order_place_after在管理員完成訂單時觸發?或者當用戶完成cekout過程時? 當管理員在向用戶發送通知後完成訂單時,需要更改 – Christian 2010-06-15 23:25:18

+0

訂單添加到系統時觸發sales_order_place_after事件。如果您在訂單移至「已完成」狀態時查找單個事件,則沒有特定事件。您應該可以使用sales_order_save_after來檢查訂單的狀態。 – 2010-06-16 02:14:41

+0

感謝您的回答約瑟夫! – Christian 2010-06-16 05:43:14

相關問題