1
use Magento\Quote\Model\QuoteManagement;
...

// Create order from quote

$order = $this->quoteManagement->submit($quote);

// This code send mail automatically

//also used **$order->setCanSendNewEmailFlag(false);** 

// But this not working in magento 2.3.0

1 Answer 1

4

Overwrite/pluginize following class which is responsible for sending order mail.

vendor/magento/module-quote/Observer/SubmitObserver.php

You can try the following using plugin:

app/code/SR/MagentoCommunity/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\Quote\Observer\SubmitObserver">
        <plugin name="SR_MagentoCommunity::stop_sending_order_email_for_custom_payment"
                type="SR\MagentoCommunity\Plugin\Quote\Observer\SubmitObserver" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Quote/Observer/SubmitObserver.php

<?php
namespace SR\MagentoCommunity\Plugin\Quote\Observer;

class SubmitObserver
{
    public function beforeExecute(
        \Magento\Quote\Observer\SubmitObserver $subject,
        \Magento\Framework\Event\Observer $observer
    ) {
        /** @var  \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();

        // Add your custom logic here
        $order->setCanSendNewEmailFlag(false);
        return [$observer];
    }
}
1
  • Hello brother, I have tried your solution with some payment and order status condition but order mail is still running in that payment cases: if($paymentCode == 'swed_m2s' || $paymentCode == 'seb_m2s' || $paymentCode == 'lhv_m2s' || $paymentCode == 'estcard_m2s' || $paymentCode == 'coop_m2s') { if($orderStatus == 'pending') { $order->setCanSendNewEmailFlag(false); } } return [$observer]; Aug 20, 2020 at 7:51

Your Answer

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

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