2016-03-25 131 views
0

我試圖在下訂單後使用magento事件觀察者重新計算magento總數。我使用下面的代碼計算總數。重新計算訂單後的總計

觀察Functon

public function recalculateTotal($observer){ 
    //var_dump($observer->getOrder()->getData());exit; 
    $orderModel = $observer->getOrder(); 
    //echo "<pre>";print_r($orderModel->getData());echo "</pre>";exit; 
    $saveModel = Mage::getModel('sales/order')->load($orderModel->getId()); 
    $saveModel->setSurcharge(10); 
    $saveModel->setBaseSurcharge(10); 
    $saveModel->collectTotals(); 
    $saveModel->save(); 
    echo "<pre>";print_r($orderModel->getData());echo "</pre>";exit; 
} 

事件

<global> 
    <events> 
     <sales_order_place_after> 
      <observers> 
      <order_surcharge_display> 
       <type>singleton</type> 
       <class>surcharge/observer</class> 
       <method>recalculateTotal</method> 
      </order_surcharge_display> 
      </observers> 
     </sales_order_place_after> 
    </events> 

</global> 

但沒有什麼變化,請幫助。

回答

0

您無法重新計算訂單總計。此方法僅適用於報價。如果您想添加個人價值,您必須使用:sales_quote_collect_totals_after事件。

在你的模塊的config.xml中你應該添加這樣的事情你總從報價轉換爲訂單:

<sales_convert_quote> 
    <surcharge_amount> 
     <to_order>*</to_order> 
    </surcharge_amount> 
    <base_surcharge_amount> 
     <to_order>*</to_order> 
    </base_surcharge_amount> 
</sales_convert_quote> 

直接變化的訂單總數:

public function recalculateTotal($observer){ 
    //var_dump($observer->getOrder()->getData());exit; 
    $orderModel = $observer->getOrder(); 
    //echo "<pre>";print_r($orderModel->getData());echo "</pre>";exit; 
    $orderModel->setSurchargeAmount(10); // usually all totals has "amount" suffix 
    $orderModel->setBaseSurchargeAmount(10); 
    $orderModel->setGrandTotal($orderModel->getGrandTotal() + 10); 
    $orderModel->setBaseGrandTotal($orderModel->getBaseGrandTotal() + 10); 
    try { 
     $orderModel->save(); 
    } catch (Exception $e) { 
     echo $e->getMessage(); 
    } 
    echo "<pre>";print_r($orderModel->getData());echo "</pre>";exit; 
} 

PS :您必須在報價單和報價單上進行更改

+0

感謝您的回放@Siarhey但這不是iw的工作原理螞蟻在下訂單後換個宏。 – Zaheerabbas

+0

答覆已更新 –