1

When I do payment using other payment methods its sending only one confirmation email one to user and one to admin , but when I use paypal as payment method it send two confirmation emails to admin and two to user ? Not sure whats happening here ?

UPDATE

I debugged more and found that I can make changes to send() method of OrderSender class , I tried changing it in core file vendor/magento/module-sales/Model/Order/Email/Sender/OrderSender.php and it worked for me .

but I don't want to make changes to the core file so I created a custom plugin to achieve this

module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="rahul_Sendorderplugin" setup_version="1.0.0"/>
</config>

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'rahul_Sendorderplugin',
    __DIR__
);

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">
    <preference for="Magento\Sales\Model\Order\Email\Sender\OrderSender" type="rahul\Sendorderplugin\Model\Order\Email\Sender\OrderSender"></preference>
</config>

OrderSender.php

<?php

namespace rahul\Sendorderplugin\Model\Order\Email\Sender;

use Magento\Sales\Model\Order;

class OrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{

    public function beforeSend(Order $order, $forceSyncMode = false)
    {
        echo "hello";exit; // but its not working
    }
}

Please suggest what am I doing wrong here .

Thanks

1

Hope this will help someone . I had fixed this issue by overriding the send() method as I mentioned in the question and checking if the email is already sent for the order or not . Below is the code snippet .

<?php
namespace rahul\Sendorderplugin\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)
    {
        $order->setSendEmail(true);

        if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) {

//$order->getEmailSent() will check if the email has been sent for order or not


            if ($this->checkAndSend($order) && !$order->getEmailSent()) {
                $order->setEmailSent(true);
                $this->orderResource->saveAttribute($order, ['send_email', 'email_sent']);
                return true;
            }
        } else {
            $order->setEmailSent(null);
            $this->orderResource->saveAttribute($order, 'email_sent');
        }

        $this->orderResource->saveAttribute($order, 'send_email');

        return false;
    }
}

This fixed the issue I was facing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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