Question

The thing is, I want to send them manually via the API because in my country a invoice is a legal-bound order. Is there a way to do that?

Thanks for your help!

Was it helpful?

Solution 2

I don't think the accepted answer is the best possible way to handle the problem. If you disable the automatic transactional emails completely from the backend, you also can't trigger them manually or use them in your custom module anymore. That means you have to create every transactional email you disabled from scratch if you need to send it at another point as magento standart, which also is a considerable maintenance effort in the aftermath.

The solution I came up with is to programmatically disable sending the email on invoice creation and leverage the default sender classes in a custom observer event. In our case we wanted to send the invoice email when shipment is created.

You do that by overriding \Magento\Sales\Model\InvoiceOrder. Locate the line

$this->notifierInterface->notify($order, $invoice, $comment);

And remove it. If you want to trigger the email, you can still do that from anywhere you want using the standard "send" function from the InvoiceSender. In our case, we trigger the email from observer like this:

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;

class SendInvoiceWithShipment implements ObserverInterface
{
    protected $_invoiceSender;

    public function __construct(
        InvoiceSender $invoiceSender
    ) {
        $this->_invoiceSender = $invoiceSender;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getShipment()->getOrder();
        $invoices = $order->getInvoiceCollection();
        foreach ($invoices as $invoice) {
             // this is where the magic happens
             $this->_invoiceSender->send($invoice);
        }               


    }
}

Observer is triggered by event sales_order_shipment_save_after

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name='sales_order_shipment_save_after'>
        <observer name='SendInvoiceWithShipment' instance='Vendor\Module\Observer\SendInvoiceWithShipment'
        />
    </event>
</config>

You can do this for every transactional email.

OTHER TIPS

go to System->Configuration->Sales Emails and disable "Invoice".

Cheerz Simon

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top