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

******************************************************************************

0.1.0MCorner_MCornerOrdersObserver_Model_ObserverMCorner_MCornerOrdersObserver_Model_ObserverafterSalesOrderSaveCommitAfterMCorner_MCornerOrdersObserver_Helper

******************************************************************************

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 {

$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

******************************************************************************

truecommunity

******************************************************************************

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.









0 comments

  • Business Center - An Overview In today's tough business scenario, organizations always look for effective ways to reduce expenses, increase productivity and improve operational efficiency. As businesses expand across geographies, it is not always possible to set up a…
  • How to Clean Windows Quickly and Effectively Believe it or not, there are plenty of ways to clean your windows incorrectly. You could end up with streaks, smudges or with a window that retains most of its dirt! With a task as tedious as cleaning windows, it's best to just do it right…
  • The Four Best Ways To Invest In Your Children's Financial Future When speaking of the four great ways to invest in your children's future, one must start at home. Childhood Training Proper upbringing is an investment that will continue paying dividends into a child's future. Some basic examples may…
  • What's an Opportunity Transfer? Not long ago, an acquaintance who works for the Los Angeles School District apparently upset his superiors and the administration with a grievance and critique. As he told me the story of his ordeal, I was blown away with what had happened to him.…
  • A Direct Path to Making Money Now! Here's a direct path to making money: Create and sell your own information products. There are lots of people who do not realize that they have particular knowledge that other people would be willing to pay for, and if that's the case…