Magento – Duplicate order confirmation emails when checking out with PayPal Express

magento-2.2.5order-emailpaypal-express

We are having an issue with duplicate order confirmation emails being sent when customer's checkout with PayPal Express. We are using Magento 2.2.5

Here's what I've found so far, bare with me as I provide some context. We have an issue with the FROM not being set on emails since 2.2.4, so we applied this fix suggested in the bug report found here. This works and our emails were once again sending, but now we've noticed that customer's who checkout using PayPal receive duplicate confirmation emails. When I disable the module, they no longer receive duplicate emails, however the emails often are rejected by mail servers because of the missing FROM. If I take the code from that module and modify the core files directly, the FROM gets properly set and the customer only receives one confirmation email.

So why then when I use this module am I receiving duplicate emails?

I ran the code through xdebug placing a breakpoint at the start of the function in the following overwrite.

<?php

namespace TMP\MageFix224\Rewrite\Sales\Model\Order\Email;

class SenderBuilder extends \Magento\Sales\Model\Order\Email\SenderBuilder
{
    /**
     * Configure email template
     *
     * @return void
     */
    protected function configureEmailTemplate()
    {
        $this->transportBuilder->setTemplateIdentifier($this->templateContainer->getTemplateId());
        $this->transportBuilder->setTemplateOptions($this->templateContainer->getTemplateOptions());
        $this->transportBuilder->setTemplateVars($this->templateContainer->getTemplateVars());
        /*Send From Email Issue #14952*/
        $this->transportBuilder->setFromByStore(
            $this->identityContainer->getEmailIdentity(),
            $this->identityContainer->getStore()->getId()
        );
    }
}

When I completed a checkout, this chunk of code was ran twice in seemingly two separate calls. Here are screenshots of the call trace first hit second hit

This happens every time you checkout with PayPal, but again if I disable the module I mentioned at the top, and run xdebug with a breakpoint in the original class function call, I do not see this behavior. And if I copy and paste the code needed for the fix into the original core files, I also do not see this behavior.

So here is where I'm stuck. I'm not sure why Magento is repeating a whole request just because I have a module that overwrites a core file using a preference in a di.xml file.

Best Answer

I don't know if this will help, but those are my findings about your issue:

It seems that the first order email is sent in \Magento\Quote\Observer\SubmitObserver::execute. As far as I understand, the email shouldn't be sent here, it should be stopped by the if (!$redirectUrl && $order->getCanSendNewEmailFlag()) { check. This is because in Paypal express the $redirectUrl shouldn't be empty. Therefore I suggest that you will try to find out why $quote->getPayment()->getOrderPlaceRedirectUrl() is empty.

Related Topic