Tutorialsplane

Magento create invoice programmatically from Order


Magento create invoice programmatically from Order : You can create invoice programmatically from an order. It is very important let us create and understand with example. For this you need order object for which you want to create invoice.


Magento create invoice programmatically from Order

First load order object using the sales order model Mage::getModel(“sales/order”)->load($orderId) and then check if invoice already created else create invoice. –

Magento create invoice programmatically from Order Example –

$order = Mage::getModel("sales/order")->load($orderId);
try {
if($order->canInvoice())
{ 
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
 
if ($invoice->getTotalQty()) { 

$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transaction = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder()); 
$transaction->save();
Mage::getSingleton('core/session')->addSuccess('Invoice Created Successfully.'); 
}else{
 Mage::getSingleton('core/session')->addError('Can not create invoice without Product Quantities'); 
 }
 
}else{
Mage::getSingleton('core/session')->addError('Can not create invoice'); 
}
}
catch (Mage_Core_Exception $e) {
 echo $e->getMessage();
}