Preventing Automatic Invoice Email Sending in Magento
In Magento 1.9.0.1, invoices are automatically sent to customers every time an order is created. However, there may be cases where you want to generate the invoice but not send it via email immediately. In this post, we’ll explore two methods to achieve this.
Method 1: Disable Invoice Email Sending
The easiest way to stop automatic invoice emails is by disabling them in your Magento configuration. Here’s how:
- Navigate to System > Configuration.
- Expand the Sales section and click on Sales Emails.
- Find the Invoice section.
- Set Enabled to No.
- Click Save Config.
This will prevent all invoices from being sent, regardless of when they are created.
Method 2: Create Invoice but Don’t Send Email
If you want more control over when invoices are sent, you can create them manually and send the email later using cron jobs or a custom script. Here’s how to create an invoice without sending it:
// Include Magento core files
require_once 'app/Mage.php';
Mage::app();
// Get order ID
$orderId = 123;
// Load order
$order = Mage::getModel('sales/order')->load($orderId);
// Create invoice
$invoice = Mage_Sales_Model_Service_Orders::Invoice($order);
$invoice->setTransactionId(false);
try {
// Save the invoice
$invoice->save();
// Capture payment
$transactionSave = Mage_Transactions_Model_Manager::getInstance()
->addTransaction(
$transaction,
array('order_payment' => $payment),
Mage_Sales_Model_Order_Payment_Transaction::TYPE_CAPTURE
);
} catch (Exception $e) {
// Handle exception if needed
}
// At this point, the invoice is created but not sent
In this example, we’re using Magento’s built-in order and invoice models to create an invoice without sending it. However, to send the email later or upon certain conditions, you would need to write additional logic.
Attaching Invoice to Order Email
If you want to attach the invoice directly to the initial order confirmation email, you can modify this process:
- Create a custom observer for the
sales_order_save_afterevent. - In your observer, check if an invoice has been created and attached it to the order’s comment or PDF file.
Here’s a basic example of how you might do this in a custom module:
class Company_Magento1901_Model_Observer
{
public function attachInvoiceToOrderComment($observer)
{
$order = $observer->getEvent()->getOrder();
$invoiceCollection = Mage::getResourceModel('sales/order_invoice_collection')
->addFieldToFilter('parent_id', $order->getId());
if (count($invoiceCollection)) {
foreach ($invoiceCollection as $invoice) {
$pdf = new Zend_Pdf();
// Generate PDF from invoice
// Attach the PDF to order comments or modify the email template
// Example: Add to order comment
$order->addStatusHistoryComment("Invoice attached", false)
->setIsVisibleOnFront(true);
}
}
}
}
Remember to add your observer to the event in your module’s config.xml file:
<config>
<global>
<events>
<sales_order_save_after>
<observers>
<company_magento1901>
<type>singleton</type>
<class>Company_Magento1901_Model_Observer</class>
<method>attachInvoiceToOrderComment</method>
</company_magento1901>
</observers>
</sales_order_save_after>
</events>
</global>
</config>
Conclusion
Preventing automatic invoice email sending in Magento requires a combination of configuration changes and custom development. For most cases, disabling the feature through the admin panel is sufficient.
However, if you need more control or want to attach invoices directly to order emails, you’ll need to write custom code using observers and Magento’s built-in models.
Always ensure your changes are tested thoroughly before deploying them in a production environment. If you’re unsure about any aspect of this process, don’t hesitate to contact our development team at https://laravelcompany.com/development-services for professional assistance.
Happy coding!