0

I am facing issue with new order emails. Magento send new order emails two times for every order.

I am not getting what is issue there.

0

create custom plugin to check if the email has been already sent or not and only sent an email if its not sent before

create module.xml file

<?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__
);

in 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 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;
    }
}

Hope this will help you and let me know if you got any issues. I will be happy to help. This is the directory structure ->

enter image description here

  • tried this but Not working :( – Magecode Dec 31 '18 at 12:57
  • sorry to hear that ,just wanted to confirm you must have enabled the module and also executed setup:di:compile command and cleaned cache. I was also facing the same issue and solved out using the same. – Rahul Makwana Dec 31 '18 at 13:00
  • I have checked all things – Magecode Dec 31 '18 at 13:05
  • not sure than :( – Rahul Makwana Dec 31 '18 at 13:07
  • you should have the directory structure like the image I have added in the answer for your new extension. – Rahul Makwana Dec 31 '18 at 14:15
0

I have debugged it and found solution after lots of debugging.

Issue was occurred due to Paytm payment module. It was also overriding code to send Emails.

I have disabled Paytm module and now issue resolved.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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