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"?>
    <config>
    	<modules>
    		<MCorner_MCornerOrdersObserver>
    			<version>0.1.0</version>
    		</MCorner_MCornerOrdersObserver>
    	</modules>
    	<global>
    		<models>
    			<MCornerOrdersObserver>
    				<rewrite>
    					<observer>MCorner_MCornerOrdersObserver_Model_Observer</observer>
    				</rewrite>
    			</MCornerOrdersObserver>
    		</models>
    		<events>
                <sales_order_save_commit_after>
                    <observers>
                        <MCornerOrdersObserver>
                            <class>MCorner_MCornerOrdersObserver_Model_Observer</class>
                            <method>afterSalesOrderSaveCommitAfter</method>
                        </MCornerOrdersObserver>
                    </observers>
                </sales_order_save_commit_after>
    		</events>
    		<helpers>
                <MCornerOrdersObserver>
                    <class>MCorner_MCornerOrdersObserver_Helper</class>
                </MCornerOrdersObserver>
            </helpers>
    	</global>
    </config>
  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
    app/code/community/MCorner/MCornerOrdersObserver/Helper/Data.php

    <?php
    
    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
    <?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 {
    
    			$this->createInvoice();
    		}
    	}
    
    	protected function createInvoice() {
    		$orderState = $this->order->getState();
    		if ($orderState === Mage_Sales_Model_Order::STATE_NEW) { // Check for state new.
    			if ($this->order->canInvoice()) {
    				$this->order->getPayment()->setSkipTransactionCreation(false);
    				$invoice = $this->order->prepareInvoice();
    				$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
    				$invoice->register();
    				Mage::getModel('core/resource_transaction')
    				   ->addObject($invoice)
    				   ->addObject($this->order)
    				   ->save();
    			}
    			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:
    <config>
        <modules>
            <MCorner_MCornerOrdersObserver>
                <active>true</active>
                <codePool>community</codePool>
            </MCorner_MCornerOrdersObserver>
        </modules>
    </config>
  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.