I have had an issue for some time in overriding the OrderSender Model in Magento 2 as a result of creating a custom payment module.
First of all i was not able to prevent email from being sent out once an order is made (wether successful or not successful), which was a problem so I decided to create a flow which would override the OrderSender using the advised process by specifying the preference file in the module i.e.
class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender{
/**
* Sends order email to the customer.
*
* Email will be sent immediately in two cases:
*
* - if asynchronous email sending is disabled in global settings
* - if $forceSyncMode parameter is set to TRUE
*
* Otherwise, email will be sent later during running of
* corresponding cron job.
*
* @param Order $order
* @param bool $forceSyncMode
* @return bool
*/
public function send(Order $order, $forceSyncMode = false)
{
$payment = $order->getPayment()->getMethodInstance()->getCode();
//allow other module or payment to proceed should in case it is not my payment module is calling for order sender.
if($payment == 'afronijapay' && !$forceSyncMode){
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;
}}
as shown above so therefore everything worked fine and i was to send the email upon completing my flow of process then called the following when the payment is successful.
//send new order email
$this->_checkout_session->setForceOrderMailSentOnSuccess(true);
$this->_orderSender->send($order, true);
Again, I found this as some proposed solution from several sources including stackoverflow/stackexchange and all.
However, in as much as this works another problem comes to mind i.e. what if someone already is overriding this class from another module and Magento 2 uses that overriden class without mine then the problem may re-occur.
I built another payment module (just a copy of the original) using different namespace and vendor name and my fear came to light as Magento 2 is ignoring one of the classes overriding the OrderSender class and using the other thereby making the problem to start re-occuring.
I will appreciate any suggestions on how to tackle this issue. Thanks.