Invoice Auto Generation in Magento

Sometimes the Magento order process causes more work than a client desires. Imagine you have a client who utilizes their Magento eCommerce store so their customers can make purchases online. Now they also want to process orders on their own with customers from their physical store.

If they were to place an order, they would need to also invoice and process shipping. This becomes cumbersome. Here we bypass this so that placing a successful order with payment will auto invoice the order so it is complete and nothing more is required.

Create NAMESPACE folder under app/code/community/MCorner

• Create module folder “MCornerOrdersObserver” as we will use this one as the name of our module

• app/code/community/MCorner/MCornerOrdersObserver

• Create Folders

• app/code/community/MCorner/MCornerOrdersObserver/etc

• app/code/community/MCorner/ MCornerOrdersObserver/Model

• app/code/community/MCorner/ MCornerOrdersObserver/Helper

• Create the config.xml file

• app/code/community/MCorner/MCornerOrdersObserver/etc/config.xml

• Inside the config.xml file copy and paste the code below




Create the main observer file:

• app/code/community/MCorner/ MCornerOrdersObserver/Model/Observer.php

• Copy and paste the code below into app/code/community/MCorner/ MCornerOrdersObserver/Model/Observer.php


class MCorner_MCornerOrdersObserver_Model_Observer {

public $order;//the order…

function afterSalesOrderSaveCommitAfter(&$event) {

return $this->__process($event);


protected function __process($event) {

$this->order = $event->getEvent()->getOrder();

if (!$this->order->getId()) {

//order is not saved in the database

return $this;


else {




protected function createInvoice() {

$orderState = $this->order->getState();

if ($orderState === Mage_Sales_Model_Order::STATE_NEW) { // Check for state new.

if ($this->order->canInvoice()) {


$invoice = $this->order->prepareInvoice();








else {

//we can not invoice it so the process is normal.







To activate the observer module create the module file:

• app/etc/modules/MCorner_ MCornerOrdersObserver.xml

• Copy and paste the code below




Make sure all files are saved in their locations as in the tutorial and now go into Magento admin and CLEAR ALL CACHE.

Now, if an order is placed through your Magento store it will automatically be invoiced and the invoice amount will be charged. An invoice will be created and an email will be sent. This functionality is similar to clicking the Invoice Button in Magento order administration.

Source by Harry Balle Singh

Leave a Reply

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

Save & Share Cart
Your Shopping Cart will be saved and you'll be given a link. You, or anyone with the link, can use it to retrieve your Cart at any time.
Back Save & Share Cart
Your Shopping Cart will be saved with Product pictures and information, and Cart Totals. Then send it to yourself, or a friend, with a link to retrieve it at any time.
Your cart email sent successfully :)