2

I have used below event to stop Invoice email notification that is triggered after the invoice creation.

Vendor/Module/etc/events.xml

<?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_invoice_save_after">
      <observer name="stop_invoice_email_send" instance="Vendor\Module\Observer\StopInvoiceEmail" />
   </event>
</config>

Vendor/Module/Observer/StopInvoiceEmail.php

 <?php
 namespace Vendor\Module\Observer;

 use Magento\Framework\Event\Observer;
 use Magento\Framework\Event\ObserverInterface;
 use Psr\Log\LoggerInterface;

 class StopInvoiceEmail implements ObserverInterface
 {
   protected $logger;

   public function __construct(LoggerInterface $logger)
    {
       $this->logger = $logger;
    }
 public function execute(Observer $observer)
 {
    try{
        $invoice = $observer->getEvent()->getInvoice();
        // Check the payment method used for the order
        $order = $invoice->getOrder();
        $payment = $order->getPayment();
        if ($payment->getMethod() === 'razorpay') { which uses the authorzse and capture
            $this->logger->info('Razorpay method inside--');
            // Prevent sending invoice email
            $invoice->setEmailSent(true);
        }
    }catch(\Exception $e){
        $this->logger->info('An error occurred: ' . $e->getMessage());
     }
   }
}

The above code is still sending the invoice email notification to the customer. Can anyone suggest how we can achieve this? Thanks in advance!!

3 Answers 3

0

Can you please try this

You can disable the invoice email in System > Configuration > Sales Emails then look for invoice and set 'enabled' to 'no'.

OR

// Prevent sending invoice email
 $invoice->setEmailSent(false); // replace true to false
2
  • Thank you for your answer, disabling the configuration will entirely stop sending the email, my requirement is to stop during the invoice generation, later we have to send invoice mail in some functionality, Thanks Aug 14 at 11:30
  • This suggestion should work: Disabling default Magento email. and you have to write a custom function that send the email by self.
    – HoangHieu
    Aug 16 at 4:44
0

Your observer is executed after the invoice is already saved, so you need to save the attribute you just changed.

And just to be sure, I would also add the "send_email" flag to false.

// $this->entityResource being : \Magento\Sales\Model\ResourceModel\Order\Invoice
$invoice->setEmailSent(true);
$invoice->setSendEmail(false);
$this->entityResource->saveAttribute(
    $invoice,
    ['email_sent', 'send_email']
);

Here would be your full Observer :

<?php
namespace Mindtree\Billdesk\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\ResourceModel\Order\Invoice;
use Psr\Log\LoggerInterface;

class StopInvoiceEmail implements ObserverInterface
{
    protected $logger;

    protected $entityResource;

    public function __construct(
        LoggerInterface $logger,
        Invoice $entityResource
    ) {
        $this->logger = $logger;
        $this->entityResource = $entityResource;
    }
    public function execute(Observer $observer)
    {
        try{
            $invoice = $observer->getEvent()->getInvoice();
            // Check the payment method used for the order
            $order = $invoice->getOrder();
            $payment = $order->getPayment();
            if ($payment->getMethod() === 'razorpay') {
                $this->logger->info('Razorpay method inside--');
                // Prevent sending invoice email
                $invoice->setEmailSent(true);
                $invoice->setSendEmail(false);
                $this->entityResource->saveAttribute(
                    $invoice,
                    ['email_sent', 'send_email']
                );
            }
        }catch(\Exception $e){
            $this->logger->info('An error occurred: ' . $e->getMessage());
        }
    }
}
7
  • getting this error, Error: Cannot instantiate abstract class Magento\Sales\Model\ResourceModel\EntityAbstract , this we can't instantiate as its a abstract class Aug 14 at 12:45
  • My bad, you need to use this class : Magento\Sales\Model\ResourceModel\Order\Invoice. I will edit the answer
    – Cladiuss
    Aug 14 at 12:50
  • For more informations, I took example at the following method : \Magento\Sales\Model\EmailSenderHandler::sendEmails
    – Cladiuss
    Aug 14 at 12:52
  • hi @Cladiuss, tried this solution still not working, email is generating also $invoice->setSendEmail(false); not updating values getting stored as 1 in table, pls try at your end once Aug 14 at 13:12
  • Unfortunately I have no local environment to test it right now. Are you sure about the "razorpay" code (do you get the log line in your log file) ?Did you try with another payment method ? Maybe razorpay module is bypassing the default email sender handler.
    – Cladiuss
    Aug 14 at 13:48
0

Create plugin in etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order\Invoice">
        <plugin name="prevent_invoice_email_notification" type="Vendor\Module\Plugin\PreventInvoiceMail" sortOrder="1" />
    </type>
</config>

Create the PreventInvoiceMail.php file in the Vendor/Module/Plugin directory:

<?php
namespace Vendor\Module\Plugin;

class PreventInvoiceMail
{
    public function beforeRegister(Magento\Sales\Model\Order\Invoice $invoice)
    {
        // Disable sending invoice email
        $invoice->setEmailSent(true);
        return [$invoice];
    }
}
7
  • Let me know, If any issue in it. Aug 16 at 11:11
  • I tried this @Divya, its not working Aug 17 at 8:59
  • setEmailSent is not updating to true, its saving as null Aug 22 at 15:13
  • Currently , Is it pending or working fine. Sep 1 at 5:36
  • HI @Divya, I implemented this with some other way, let me post the answer Sep 1 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.