magento 2 order confirmation email sending before payment gatway redirection
I was facing same issue but I found alternative solution like I turned off Magento default Order Confirmation email and Send Order Confirmation Email When Order is being placed Successfully.
Here are the step to follow :
Create Observer Event which being called after order placed :
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="checkout_onepage_controller_success_action">
<observer name="checkout_onepage_controller_success_action_sendmail" instance="<vendor_name>\OrderSuccessMail\Observer\SendMailOnOrderSuccess" />
</event>
</config>
Observer: SendMailOnOrderSuccess.php
<?php
namespace <vendor_name>\OrderSuccessMail\Observer;
use Magento\Framework\Event\ObserverInterface;
class SendMailOnOrderSuccess implements ObserverInterface
{
/**
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderModel;
/**
* @var \Magento\Sales\Model\Order\Email\Sender\OrderSender
*/
protected $orderSender;
/**
* @var \Magento\Checkout\Model\Session $checkoutSession
*/
protected $checkoutSession;
/**
* @param \Magento\Sales\Model\OrderFactory $orderModel
* @param \Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender
* @param \Magento\Checkout\Model\Session $checkoutSession
*
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Sales\Model\OrderFactory $orderModel,
\Magento\Sales\Model\Order\Email\Sender\OrderSender $orderSender,
\Magento\Checkout\Model\Session $checkoutSession
)
{
$this->orderModel = $orderModel;
$this->orderSender = $orderSender;
$this->checkoutSession = $checkoutSession;
}
/**
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$orderIds = $observer->getEvent()->getOrderIds();
if(count($orderIds))
{
$this->checkoutSession->setForceOrderMailSentOnSuccess(true);
$order = $this->orderModel->create()->load($orderIds[0]);
$this->orderSender->send($order, true);
}
}
}
Here $this->orderSender->send($order, true); called which extends abstract class method() which check if Order confirmation email is enabled from backend or not so we need create plugin for that
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\Email\Container\OrderIdentity">
<plugin name="change_is_enable_method" type="\<vendor_name>\OrderSuccessMail\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
</type>
</config>
OrderIdentityPlugin.php
<?php
namespace <vendor_name>\OrderSuccessMail\Plugin\Sales\Order\Email\Container;
class OrderIdentityPlugin
{
/**
* @var \Magento\Checkout\Model\Session $checkoutSession
*/
protected $checkoutSession;
/**
* @param \Magento\Checkout\Model\Session $checkoutSession
*
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Checkout\Model\Session $checkoutSession
)
{
$this->checkoutSession = $checkoutSession;
}
/**
* @param \Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject
* @param callable $proceed
* @return bool
*/
public function aroundIsEnabled(\Magento\Sales\Model\Order\Email\Container\OrderIdentity $subject, callable $proceed)
{
$returnValue = $proceed();
$forceOrderMailSentOnSuccess = $this->checkoutSession->getForceOrderMailSentOnSuccess();
if(isset($forceOrderMailSentOnSuccess) && $forceOrderMailSentOnSuccess)
{
if($returnValue)
$returnValue = false;
else
$returnValue = true;
$this->checkoutSession->unsForceOrderMailSentOnSuccess();
}
return $returnValue;
}
}
Here in above plugin we have checked if confirmation is enabled from backend config or not, If yes then It works as default but if No then our custom email Confirmation order will be sent.
Hope it helps.
This can be achieved by rewriting
Magento\Sales\Model\Order\Email\Sender\OrderSender::send
function, For that first add the following in your di.xml
<preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender" type="Company\Module\Model\Order\Email\Sender\OrderSender"/>
and Create class OrderSender with as below
namespace Company\Module\Model\Order\Email\Sender;
use Magento\Sales\Model\Order;
class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender {
public function send(Order $order, $forceSyncMode = false)
{
$payment = $order->getPayment()->getMethodInstance()->getCode();
if($payment == 'your-payment-method-code'){
return false;
}
$order->setSendEmail(true);
if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {
if ($this->checkAndSend($order)) {
$order->setEmailSent(true);
$this->orderResource->saveAttribute($order, ['send_email', 'email_sent']);
return true;
}
}
$this->orderResource->saveAttribute($order, 'send_email');
return false;
}
}
Once you return back to site from payment gateway send confirmation mail manually from your success controller.