3

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. – Khoa TruongDinh Feb 5 '17 at 9:32
2

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

Step 1 :

Insert below event to your config.xml

<events>
        <sales_order_place_after>
            <observers>
                <yourmodule_autoinvoice>
                    <class>yourmodule_autoinvoice/observer</class>
                    <method>autoInvoice</method>
                </yourmodule_autoinvoice>
            </observers>
        </sales_order_place_after>
</events>

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()));
                $orders->getSelect()->limit(1);

                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);
                            $order->save();
                        } else {

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

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

                            $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                            $invoice->register();

                            $transactionSave = Mage::getModel('core/resource_transaction')
                                               ->addObject($invoice)
                                               ->addObject($invoice->getOrder());
                            $transactionSave->save();

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

                            $invoice->getOrder()->setIsInProcess(true);
                            $order->addStatusHistoryComment(Mage::helper('yourmodule')->__('Auto Invoice generated.'), 
                                                                Mage_Sales_Model_Order::STATE_PROCESSING)->setIsCustomerNotified(true);
                            $invoice->sendEmail(true, '');
                            $order->save(); 
                        }

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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