2015-05-20 152 views
1

我需要在訂單收到processing狀態後自動創建發票。在我的擴展中應該遵守哪個事件來實現這個目標?在Magento訂單狀態更改事件上觸發觀察者


我在嘗試,但這段代碼不工作,它可能是什麼?

的.xml在等/模塊

<?xml version="1.0"?> 
<config> 
<modules> 
    <Atwix_Orderhook> 
     <active>true</active> 
     <codePool>community</codePool> 
    </Atwix_Orderhook> 
</modules> 
</config> 

config.xml中在app /代碼/ ...

<?xml version="1.0"?> 
<config> 
<modules> 
    <Atwix_Orderhook> 
     <version>1.0</version> 
    </Atwix_Orderhook> 
</modules> 
<global> 
    <models>    
     <orderhook> 
      <class>Atwix_Orderhook_Model</class> 
     </orderhook> 
    </models> 
    <events> 
     <sales_order_save_after> 
      <observers> 
       <auto_invoice_order> 
        <type>singleton</type> 
        <class>Atwix_Orderhook_Model_Observer</class> 
        <method>implementOrderStatus</method> 
       </auto_invoice_order> 
      </observers> 
     </sales_order_save_after> 
    </events> 

</global> 
</config> 

Oberver.php在應用程序/代碼/ ...

<?php 
class Atwix_Orderhook_Model_Observer 
{ 
public function implementOrderStatus($event) 
{ 
    $order = $event->getOrder(); 

    if ($order == 'processing') { 
     if ($order->canInvoice()) 
      $this->_processOrderStatus($order); 
    } 
    return $this; 
} 
         private function _processOrderStatus($order) 
         { 
          $invoice = $order->prepareInvoice(); 

          $invoice->register(); 
          Mage::getModel('core/resource_transaction') 
           ->addObject($invoice) 
           ->addObject($invoice->getOrder()) 
           ->save(); 

          $invoice->sendEmail(false, ''); 
          return true; 
         } 
} 

回答

6

您可以觀察到sales_order_save_after,然後檢查您的定位狀態,如下所示:

public function exampleEventHandler(Varien_Event_Observer $observer) 
{ 
    /* @var Mage_Sales_Model_Order $order */ 
    $order = $observer->getOrder(); 
    $stateProcessing = $order::STATE_PROCESSING; 
    // Only trigger when an order enters processing state. 
    if ($order->getState() == $stateProcessing && $order->getOrigData('state') != $stateProcessing) { 
     ... 
    } 
    ... 
} 
+0

爲了獲得狀態,我分別使用了'$ order-> getStatus()'和'$ order-> getOrigData('status')'。 – electroid