1

I want to generate invoice automatically when I create the shipment for the order manually. How can I create a system like this. Does anyone know how to do this? Any assistance will be helpful.

4

I'd go the route of creating an event-observer for sales_order_shipment_save_after:

So in app/code/local/Namespace/Module/etc/config.xml:

...
<events>
    <sales_order_shipment_save_after>
    <observers>
        <namespace_module>
        <type>singleton</type>
        <class>Namespace_Module_Model_Observer</class>
        <method>autoInvoice</method>
        </namespace_module>
    </observers>
    </sales_order_shipment_save_after>
</events>
....

and in app/code/local/Namespace/Module/Model/Observer.php:

<?php
class Namespace_Module_Model_Observer
{
   public function autoInvoice($observer) {
       $shipment = $observer->getEvent()->getShipment();
       $order = $shipment->getOrder();
       if($order->canInvoice())
       {
           $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
           $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
           $invoice->register();
           $transactionSave = Mage::getModel('core/resource_transaction')
           ->addObject($invoice)
           ->addObject($invoice->getOrder());
           $transactionSave->save();
       }
   }
}

Then you should be all set.

  • Very nice code, works perfectly for me as well. Can we also make it to capture the payment online automatically when invoice created? – Magento Learner Jul 26 '17 at 10:19
  • Thanks @xxx, the code should be capturing your online payment ->setRequestedCaptureCase(...) and the transactionSave->save();. Check your log files if this is not working for you. – Moose Jul 27 '17 at 6:09

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.