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:-

That’s all. Your invoice for the order is created.

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS 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?

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

    Thanks anyway.

  • 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.

  • 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')

    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()