1

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.

1 Answer 1

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>";
2
  • Yes we can this but this is not ideal way to change grandtotal like this may be magento has good way to do this.
    – Zahirabbas
    Commented Mar 28, 2016 at 6:43
  • Good way is convert the quote to the order. In other way you should calculate base totals in your code. Commented Mar 28, 2016 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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