Skip to content

Magento: Create & Send Invoice Programatically

this is short script to create an invoice & send email invoice to the client, here you go

$orderId = 250;

$order = Mage::getModel(‘sales/order’)->load($orderId);
try {
if(!$order->canInvoice())
{
Mage::throwException(Mage::helper(‘core’)->__(‘Cannot create an invoice.’));
}
$invoice = Mage::getModel(‘sales/service_order’, $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper(‘core’)->__(‘Cannot create an invoice without products.’));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$transactionSave = Mage::getModel(‘core/resource_transaction’)
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoice->sendEmail();

/* SET Order Status Here */
$orderModel = Mage::getModel(‘sales/order’);
$orderModel->load($orderId);
$orderModel->setStatus(‘complete’)
->save();

}
catch (Mage_Core_Exception $e) {

echo $e->getMessage();
}

Share

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.