Magento Auto Invoice Orders

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.

  1. Create NAMESPACE folder under app/code/community/MCorner
  2. Create module folder “MCornerOrdersObserver” as we will use this one as the name of our module
    • app/code/community/MCorner/MCornerOrdersObserver
  3. Create Folders
    • app/code/community/MCorner/MCornerOrdersObserver/etc
    • app/code/community/MCorner/ MCornerOrdersObserver/Model
    • app/code/community/MCorner/ MCornerOrdersObserver/Helper
  4. Create the config.xml file
    • app/code/community/MCorner/MCornerOrdersObserver/etc/config.xml
  5. Inside the config.xml file copy and paste the code below:
    <?xml version="1.0"?>
  6. Create Helper file so that any helper functions can be placed in this class.
    • app/code/community/MCorner/MCornerOrdersObserver/Helper/Data.php
  7. Copy and paste the code below into

    class MCorner_MCornerOrdersObserver_Helper_Data extends Mage_Core_Helper_Abstract {
  8. Create the main observer file:
    • app/code/community/MCorner/ MCornerOrdersObserver/Model/Observer.php
  9. 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.
  10. To activate the observer module create the module file:
    • app/etc/modules/MCorner_ MCornerOrdersObserver.xml
  11. Copy and paste the code below:
  12. 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.

34 thoughts on “Magento Auto Invoice Orders

  1. Increible! You module works perfect, you are the best. But one question, How can I send notify mails for customers when the invoice are created?
    Thanks so much

    • I have the answer! But now I have problems with Paypal order and Invoice! This is a eternal loop!

      • Hey Jaime, glad you liked it :).

        About the PayPal issue, can you give additional details on which PayPal option you are using?

        I think it maybe that paypal is not setting the proper state to the order and that is why the checks for order state and ‘can invoice’ are failing.

        You can try the following,

        if ($orderState === Mage_Sales_Model_Order::STATE_NEW) { // Check for state new.
        if ($this->order->canInvoice()) {
        if ($orderState === Mage_Sales_Model_Order::STATE_NEW && !$this->order->getIsProcessedByMCornerOrdersObserver()) { // Check for state new.
        if ($this->order->canInvoice()) {

        This should set a variable to the order and the next time it passes, it would know that it was already processed by the observer.

      • Your module works perfect, Its so great work. The new problem that I have it’s for other thing because your module create invoice automatically when one order are created, but my problem it’s when I pay with paypal the invoice are created automatically (for IPN) but not send the notify email.
        Your job its great my problem its for other thing. Thanks so much!

  2. Hi Jamie,

    Yeah email problems are a pain :(…

    So, “order confirmation” emails should be sent by default but “Copy of Invoice” when invoice is created by default are not sent. Invoices require additional setting and I can probably post a quick how to programatically issue an invoice and send copy of invoice as long as your server is not the problem causer.

    If no emails are sent out by Magento, follow this tutorial to see whether the issue is with magento or your server:

  3. I just tested your module with Magento 1.6.2 is it did not work.
    I did a simulation on a free downloadable product but the status is pending for the customer.

    • Can you test using a normal product with some test payment method to make sure that the module is activated properly?

      We have tested it on 1.6.2 and it was working, it could be some specific setting in Magento is causing this to happen.

      Make sure you check checking out with a test credit card like, you can get a developer account and get access details and do a test.

      Other thing you can do is, inside the observer file(app/code/community/MCorner/MCornerOrdersObserver/Model/Observer.php), you place a die command after the start of the file(

  4. Hi I made all the steps you posted but after pushing the checkout button I havent been redirected to paypal or sofortueberweisung (payment solution which I use). In this case I cannot complete the payment and auto invoice didn’t work. Do you have any idea what could be the reaseon for that misconfiguration?

  5. I am sorry you are having this problem. The extension above listens for an order being payed for and no invoice was issued for it. In that case it will try to issue and invoice as if you have clicked the Invoice button inside magento admin.

    I can not tell for sure what is going on.
    1. What happens when you click the Checkout button (is the /checkout/onepage blank)?
    2. Where does the checkout fail(which step of checkout -> Billing, Payment, Order Review Submit Order)?
    3. Does it all work with the module above not on the system aside from the fact orders are not completed automatically.

    Let us know more info and maybe we can give you pointers as to what could it be the cause. And check error logs /var/log/system.log and exception.log for any errors that occur during the checkout.

  6. This is working like a Charm.

    Just 1 question Can we print it as a plain HTML insted of a pdf format.
    I want to open the invoice in a plain HTML format to print the invoice and does not want to save it as .pdf and open the pdf to print?
    Is it possible?
    Thanks once again for the great post…

  7. Hello Stevo,

    You just made my day. I quoted a developer and he asked $100 to fix this. But lucky I stumbled upon this post. Everything works! I do not understand one line of code/coding, but simply followed the instructions and everything works. Thanks Stevo!

    FYI: I’m using magento CE

  8. Hi,
    I installed this module. when I placed order it gives following error.

    Fatal error: Maximum execution time of 30 seconds exceeded in /storage/public_html/magento- on line 624

    • HI,

      This seems to be a php execution time error caused by the script running too long.

      Please answer the below questions so that we can assist you further:

      Is it possible that the system you are under has limited resources (RAM & CPU) that causes for the script to operate this long?

      Did this appear as an issue after you have installed this module?

      Did the checkout process took as long as it does atm > 30 sec?

    • Hi,

      I am a bit unclear on what you mean by in a “new window”?

      Do you mean to open in a new window as a html instead of pdf or just have the pdf to open instead of being prompted to download it?

      Generally, using pdf ensures that document always appears the same no matter what browser, OS, printer etc you are using and I think that is the main reason Magento devs initially made it to work this way. Magento does not support printing/generating invoice for print other than in pdf.

      But both cases mentioned above would require extending the core controller/model/blocks/template of the magento sales module.

      This needs to be developed additionally. I would suggest that you look into extending the sales/order controller, blocks and template files to have Magento show an html page inside a new window or show the pdf instead of prompting you to download it.

      This is quite detailed to do and not the scope of this post but we may revisit this in future so stay tuned to our site.

  9. Pingback: Magento Auto Invoice Orders « MAGESAM

  10. Great module! One question. How about after invoicing to change order state to “Processing” or completed?
    What would be the code?

    Thank you, it works like a charm.

    • The state of the order should automatically be set to ‘Complete’ once it has been invoiced. This extension would do that.

  11. Thank you for this great module.
    It works great, just the invoice email that won’t send.
    I even tried using the other code you commented.
    I am not using paypal, but yeah, invoice is created, but not sent.
    Any ideas?


      • Hey there.
        Only thing I added to the code was:

        On file Observer.php where protected function createInvoice() { is.
        Just right after save.
        Is there a way to make it work for only one payment way? Or only when I set payment made, or create invoice?
        I don’t want it to create invoice for one kind of payment and when I actually created the invoice, it would email too.


  12. Eric,
    To filter by payment types, you can add something like the following:
    if ($this->order->getPayment()->getMethod() === ‘authorizenet’){
    // do something here

    This is an example for but can be used for all payment types.

    • Thanks! But I had to do other way:

      Added line:
      $paymethod = $this->order->getPayment()->getMethod();
      And this:
      if ($orderState === Mage_Sales_Model_Order::STATE_NEW && $metodo === ‘PAYMENT’ && !$this->order->getIsProcessedByMCornerOrdersObserver()) { // Check for state new.

      And that’s it!
      I tried changing the STATE_NEW to STATE_PROCESSING, because it’s the only payment that has state of processing, but it didn’t work.

      • Code was:
        if ($orderState === Mage_Sales_Model_Order::STATE_NEW && $paymethod === ‘PAYMENT’ && !$this->order->getIsProcessedByMCornerOrdersObserver()) { //

  13. Hi there again.
    The module will still invoice payments that fail and send email to customer…
    Is there a way to not invoice the order (payment method already does this), but only send the email?

    • Eric,
      You can try to send order email with:

  14. Great stuff! How could I alter this code to prevent an auto-generated invoice for purchase orders? My credit card orders, handled by paypal and paypal purchases are auto-generated and that’s perfect, but I don’t want this for purchase orders.

    • Hi Chris,
      You should filter by payment type.

      If ($this->order->getPayment()->getMethod() != “Purchase Order”){
      // then generate invoce – put invoice code here

      Or you can add it to existing if statement:

      if ($orderState === Mage_Sales_Model_Order::STATE_NEW)

      replace with

      if ($orderState === Mage_Sales_Model_Order::STATE_NEW && $this->order->getPayment()->getMethod() != “Purchase Order”)


Comments are closed.