Home » Magento1 June 2010

Magento: Quick way to create order invoice programmatically

Here is a quick and easy way to create order invoice programmatically.

Suppose, $_order contains your order.

Here goes the code to create invoice:-

if($_order->canInvoice()) {
	/**
	 * Create invoice
	 * The invoice will be in 'Pending' state
	 */
	$invoiceId = Mage::getModel('sales/order_invoice_api')
						->create($_order->getIncrementId(), array());
	
	$invoice = Mage::getModel('sales/order_invoice')
						->loadByIncrementId($invoiceId);
	
	/**
	 * Pay invoice
	 * i.e. the invoice state is now changed to 'Paid'
	 */
	$invoice->capture()->save();	
}

That’s all. Your invoice for the order is created. Isn’t that easy :)

Hope this helps. Thanks.

From Mukesh Chapagain's Blog, post Magento: Quick way to create order invoice programmatically

php magento mukesh chapagain

Get New Post by Email

Find me on

Facebook Twitter Google+ LinkedIn RSS Feed
  • Mike G

    Nice. Thanks.

    Is there an easy way to print an invoice or packing slip BEFORE invoicing?

    Mike G

  • jmnieto

    Where can I use this code to create invoices manually?

  • http://phpbugs.wordpress.com Lijith

    Thanks. This helps.
    Also i want to send the confirmation mail to the customer on invoice.
    Please help if possible

    Thanks anyway.

  • http://www.laurent-sarrazin.com Laurent Sarrazin

    Thank you. It’s been helpful.
    However, I’ve found that it’s not necessary to use canInvoice() as the create() method already perform the check.

  • http://www.munyah.com munyah

    Thanks for the snippet, pointed me in the right direction. It turns out that using your snippet the order status will be modified to ‘processing’ and I wanted it to be complete.

    So I ended up using:

    $invoiceId = Mage::getModel('sales/order_invoice_api')
    ->create($order->getIncrementId(), array(), 'Invoice Created', true, true);

    $invoice = Mage::getModel('sales/order_invoice_api')
    ->capture($invoiceId);

    Good thing the capture from API doesn’t change the order status, yey!

  • javier

    Works great, but how can the invoice notification be sent automatically to the customer after the creation?
    Hi Lijith, could you make the code send to the customer?

  • Mjwwiggy

    which php file do we place this bit of code?

  • Dgentodent

    if we need to create invoice from root directory or conducting some kind unit testing I think invoice api doesn’t work. Another cool code goes here that works all over the magento from any location:

    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_ONLINE);$invoice->register();$transactionSave = Mage::getModel(‘core/resource_transaction’)->addObject($invoice)->addObject($invoice->getOrder()); $transactionSave->save();}catch (Mage_Core_Exception $e) { }

  • Issie81

    i want to create invoice also manual

  • Pingback: 2Coding Magento: Quick way to create order invoice programmatically | 2Coding