0

I m trying to recalculate magento total using magento event observer after placing order.I m use below code to calculate total.

Observer 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;
}

Event

     <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>

But this is not working Please help.

0

You need to change all order totals directly in your method:

$orderModel = $observer->getOrder();
$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>";
  • Yes we can this but this is not ideal way to change grandtotal like this may be magento has good way to do this. – Zaheerabbas Mar 28 '16 at 6:43
  • Good way is convert the quote to the order. In other way you should calculate base totals in your code. – Siarhey Uchukhlebau Mar 28 '16 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.