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

  • How to Use Your Phone for Music in Your Car Smartphones have become an essential item for so many millions of people around the world. Many are completely lost without their trusted phone in their hand. It is anticipated that by December 2013 there will be over 1.4 million smartphones…
  • Trying To Generate Leads Online Can Be Extremely Frustrating, But Do not Quit! Trying to generate leads online can be most frustrating for a person that is just starting in the online marketing business. Frustration can be debilitating for any person new to working in the technical field of internet network marketing for…
  • Why Business Owners Should Plan on the ObviousSome things are just obvious.Seen that new study about how a dog is as smart as a typical 2-year-old? If you're a parent of a 2-year-old, I'm sure that this comes as no surprise. And was anyone surprised that the governor of South Carolina who…
  • Cat Houses - Green Feline Living When you think of making efforts to go green in your home, what does that entail? We are in a time when many people are doing more to try and become more environmentally friendly and leave less of a carbon footprint. This may mean you make sure…
  • One More Article About Auto Insurance What Is Auto Insurance? Auto insurance is a product that is purchased and also that can pay for damages to your vehicle or others' vehicles in the event of a car accident or non-accident related harm. Each state has its laws regarding the…