I have Magento webshop, and I sell virtual products, with API all my products get send out automatically when the order is invoiced / completed.
It would be a lot easier if It could complete / invoice all orders automatically, is this possible? It's my first time using Magento, so I'm a little blank at this point.

I'm using Magento 1.9.3.

  • 1
    You can use event Observer - sales_order_save_after - to catch the state of the order and then try to create invoice programmatically. Feb 5, 2017 at 9:32

1 Answer 1


For this you need to create one small module that works with the observer.

Step 1 :

Insert below event to your config.xml


then in observer event function use the logic to generate invoice and update order status.

class Yourmodule_Autoinvoice_Model_Observer
        public function autoInvoice($observer) 
                // loading placed order using observer.

                $order = $observer->getEvent()->getOrder();
                $orders = Mage::getModel('sales/order_invoice')->getCollection()->addAttributeToFilter('order_id', array('eq'=>$order->getId()));

                if ((int)$orders->count() !== 0) {
                    return $this;

                try {
                        // checking the order can invoice or not.

                        if(!$order->canInvoice()) {
                            $order->addStatusHistoryComment('AutoInvoice: Order cannot be invoiced.', false);
                        } else {

                            $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();

                            // below capture method depends on your payment method.
                            // here I used CAPTURE_OFFLINE method.


                            $transactionSave = Mage::getModel('core/resource_transaction')

                            // Now its setting the status to processing. 
                            // for virtual products this might not required. 
                            // please test only with invoice generate code.

                            $order->addStatusHistoryComment(Mage::helper('yourmodule')->__('Auto Invoice generated.'), 
                            $invoice->sendEmail(true, '');

                    } catch (Exception $e) {
                        $order->addStatusHistoryComment('AutoInvoice: Exception occurred during autoInvoice action. Exception message: '.$e->getMessage(), false);
            return $this;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.