ylliX - Online Advertising Network Send pdf automatically when invoice is created in Magento - Yet Another Programmer's Blog

Send pdf automatically when invoice is created in Magento

It is a bit different then method showed in previous post – this time its not transactional email sent at demand, but we will catch event fired after invoice is generated. So (I suppose you have your module already) in etc/config.xml add:

<global>	
	<events>
		<sales_order_invoice_save_after>
			<observers>
				<yourvendor_yourmodule_invoice_observer>
					<class>YourVendor_YourModule_Model_Observer</class>
					<method>sendInvoice</method>
				</yourvendor_yourmodule_invoice_observer>
			</observers>
		</sales_order_invoice_save_after>			
	</events>		
</global>

Then create your class at app/code/local/YourVendor/YourModule/Model/Observer.php (or use existing one) and add new method:

public function sendInvoice($observer) {
	try {
		/* @var $order Magento_Sales_Model_Order_Invoice */
		$invoice = $observer->getEvent()->getInvoice();
		$invoice->sendEmail();
	} catch (Mage_Core_Exception $e) {
		Mage::log("Error: " . $e->getMessage());
	}
	return $this;
}

And invoice will be attached to email defined at:

Zrzut ekranu 2015-01-14 o 19.12.17

Leave a Reply