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 <?xml version="1.0"?> <config> <modules><mcorner_mcornerordersobserver> <version>0.1.0</version> </MCorner_MCornerOrdersObserver> </modules> <global> <models> <rewrite>
<observer>mcorner_mcornerordersobserver_model_observer</observer> </rewrite> </models> <events> <sales_order_save_commit_after> <observers> <class>mcorner_mcornerordersobserver_model_observer</class> <method>aftersalesordersavecommitafter</method> </observers> </sales_order_save_commit_after> </events> <helpers> <class>mcorner_mcornerordersobserver_helper</class> </helpers> </global> </config> 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
<?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.?> To activate the observer module create the module file: app/etc/modules/mcorner_ MCornerOrdersObserver.xml Copy and paste the code below <config> <modules> <MCorner_MCornerOrdersObserver> <active>true</active> <codepool>community</codepool> </MCorner_MCornerOrdersObserver> </modules> </config> 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.
About Ray Business Technologies Pvt Ltd - Ray Business Technologies Pvt Ltd, www.raybiztech.com, is a global Information Technology (IT) Services and Solutions company. Raybiztech offers comprehensive end-toend IT Services for Business Application Development & Maintenance, Enterprise Solutions, Testing & Quality Assurance, Embedded Systems, Cloud Computing and IT Infrastructure Management to organizations in the Banking & Financial Services, Insurance & Healthcare, Life Sciences, Manufacturing, Retail, Distribution & Logistics, Media & Entertainment, Leisure & Travel, Communication, Energy & Utilities, Federal Government verticals and Independent Software Vendors. Raybiztech's innovative IT Solutions in Enterprise Resource Planning (ERP), Enterprise Portals and Content Management (EPCM), Customer Relationship Management (CRM), and Enterprise Mobility including Product Development and Life-Cycle Support, have resulted in significant strategic and cost advantage for their clients worldwide. Raybiztech is now leading the way in powering next-generation enterprises with its Cloud, Mobility, Big Data and Social Media solutions. Our excellent team of Technology Professionals work with enterprise clients in North America, Latin America, Australia, Europe, Middle East and Asia.