php Invoice code not updating correctly the order
I can't actually see why, but my invoice code doesn't actually show the correct values for the totals when it get completed without errors.
The code is:
public function invoice($realOrderId){
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($realOrderId);
$invoice = Mage::getModel('sales/service_order', $orderObj)->prepareInvoice();
$invoice->addComment('Automatic invoice', false);
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoice->sendEmail();
$invoice->setEmailSent(true);
$invoice->save();
}
The code works without generating errors but the totals in the order details doesn't show that the payment has been captured!
Any suggestion about what could be the i开发者_如何学Gossue?
Allright, I managed to make the code works as intended, here the corrected function incase someone else might need it:
public function invoice($realOrderId){
$orderObj = Mage::getModel('sales/order')->loadByIncrementId($realOrderId);
$invoice = Mage::getModel('sales/service_order', $orderObj)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoice->addComment('Automatic Invoice', false);
$invoice->sendEmail();
$invoice->setEmailSent(true);
$invoice->save();
$orderObj->addStatusHistoryComment(Mage::helper('<your helper name>')->__('Automatic Invoice: '.$invoice->getIncrementId()));
$orderObj->save();}
Enjoy.
Edit: Forgot to correct the code, to avoid "errors" when Invoicing virtual products, the status can be omitted when the code set a comment in the status history.
精彩评论