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.
1 Answer
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? Jul 26, 2017 at 10:19
-
Thanks @xxx, the code should be capturing your online payment
->setRequestedCaptureCase(...)
and thetransactionSave->save();
. Check your log files if this is not working for you.– MooseJul 27, 2017 at 6:09 -