cancel
Showing results for 
Search instead for 
Did you mean: 

Send order confirmation email after successful payment

Send order confirmation email after successful payment

Hello,

 

I am facing a problem regarding order confirmation email. The order confirmation email is sending once the customer redirected to payment gateway page and order is created with pending status. I want to send this email after a successful transaction.

4 REPLIES 4

Re: Send order confirmation email after successful payment

bump

Re: Send order confirmation email after successful payment

Can someone help on this please?

Re: Send order confirmation email after successful payment

Hello @chathuramk,

 

As you mentioned your problem, I think default Magento sends the confirmation mail automatically once the order is placed. But it does not have the functionality of sending email after getting successful payment from third-party gateways. But you can try this below solution.

 

First, you need to bind Magento 2 observer with an event of checkout_onepage_controller_success_action.

Please make sure that the order confirmation email is not sent twice, So you have to create a simple method isEnable() using OrderIdentity.php.

First, you have to register the event under events.xml using below code
[Package_Name]\[Module_Name]\etc\frontend\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="[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess" />
    </event>
</config>

 

when the event is registered, you need to create an Observer class in the below file.
[Package_Name]\[Module_Name]\Observer\SendMailOnOrderSuccess.php

<?php
 
namespace [Package_Name]\[Module_Name]\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);
        }
    }
}

Once you complete adding observer code, an email will be sent to customers on successful order payment.

If you want to avoid order duplication Email, you can create a plugin using the di.xml file.
[Package_Name]\[Module_Name]\etc\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="\[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\OrderIdentityPlugin"/>
    </type>
</config>

At last, you need to create another file with ‘di.xml’ that is OrderIdentityPlugin.php to avoid email duplication.
[Package_Name]\[Module_Name]\Plugin\Sales\Order\Email\Container\ OrderIdentityPlugin.php

<?php
 
namespace [Package_Name]\[Module_Name]\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;
    }
}

Hope this will help you.

Re: Send order confirmation email after successful payment

Hi @chathuramk,

 

By default, Magento email will be sent as soon as the order is created.

 

You can use plugin to block email sending in this step. Then when your order has been paid successfully you can email as usual.

 

You can use the following plugin to block:

<type name="Magento\Sales\Model\Order\Email\Sender\OrderSender">
        <plugin disabled="false" name="prevent_send_mail_when_pending" type="Name\Module\Plugin\ConfirmationMail" sortOrder="1" />
 </type>

Hope this can help you! Let me know if you need further assistance.

_________

If issue solved, Click Kudos & Accept as Solution.

LitExtension - #1 Shopping Cart Migration Expert

LitExtension helps store owners and agencies migrate all important data from one eCommerce platform to another accurately, securely and at the highest speed.

Visit website: http://litextension.com/