Featured Articles
Default Magento automatically sends an order confirmation email once the order is placed. But what if someone wants to send an email after a successful payment completion from the third-party payment gateways in Magento 2 or what if the card is declined and payment fails? Unfortunately, Default Magento does not provide the functionality of sending email after getting successful payment from third-party gateways so we have come up with a custom code to send Email manually using event observer.
If you want to send the email only after the successful payment then you must write code in observer for checkout_onepage_controller_success_action event.
Create a simple method isEnable() using OrderIdentity.php to ensure that the order confirmation email is not sent twice.
Register the event under events.xml using the 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>
Once the event is registered, create Observer class in the following 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);
}
}
}
After adding observer code, an email will be sent to customers on successful order payment. To avoid order duplication Email, we need to 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>
And Lastly, you need to create another file along with ‘di.xml’ that is OrderIdentityPlugin.php to remove duplication of email.
[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;
}
}
That’s it! Now start sending the order confirmation Emails to your customers only after the payment is successful! Hope this code will help you to send Order Confirmation Email to your Customers after Successful Payment Transaction. You can even use & customize this code according to your need.
Related Best Magento Extension for your eCommerce Business-
Bundle Product Option Image Extension For Magento 2
Reindex from Backend for Magento 2 Extension
Contact Us Inquiry Manager for Magento 2
Contact-
Email - support@mageefy.com
Visit - https://www.mageefy.com/
Address - 72, Second Floor, Ganesh Vihar Extension, Sirsi Road, Jaipur - 302034, India.
(+91) 0141- 6693741
Article source: https://article-realm.com/article/Arts-Entertainment/Celebrities/34450-How-to-Send-Magento-2-Order-Confirmation-Email-After-Payment-Success.html
Comments
Reviews
Most Recent Articles
- Jun 4, 2026 Famous Astrologer in Kolkata for Accurate Online Astrology Consultation by Alzbeta Berka
- Jun 4, 2026 Genuine Astrologer in Kolkata for Trusted Guidance and Life Solutions by Alzbeta Berka
- Jun 4, 2026 Astrology Services in Kolkata for Accurate Guidance and Life Solutions by Alzbeta Berka
- Jun 4, 2026 Best Astrologer Services in Kolkata for Holistic Astrology and Vastu Solutions by Alzbeta Berka
- May 4, 2026 Best Astrologer Services in Kolkata for Vastu & Holistic Guidance by Alzbeta Berka
Most Viewed Articles
- 3604 hits Bike Learning & Riding While Using Motorcycle Boots Houston & Leather Accessories by Motorcycle Accessories
- 2154 hits Shop a variety of trendy Muslim headscarf online by Lisa Stewart
- 2071 hits What To Know About Musician's Union Fees by Michael Welsh Productions
- 2066 hits Which is a right place to buy the 3D business cards? by E-plasticcards
- 1570 hits Importance of Custom-Designed Business Signs by Harry Landri
Popular Articles
In today’s competitive world, one must be knowledgeable about the latest online business that works effectively through seo services....
80911 Views
Walmart is being sued by a customer alleging racial discrimination. The customer who has filed a lawsuit against the retailer claims that it...
46824 Views
Are you caught in between seo companies introduced by a friend, researched by you, or advertised by a particular site? If that is...
36987 Views
Facebook, the best and most used social app in the world, has all the social features you need. However, one feature is missing. You cannot chat...
23315 Views
If you have an idea for a new product, you can start by performing a patent search. This will help you decide whether your idea could become the...
14513 Views
Moving becomes easy when you have the right moving accessories. These moving accessories help secure and protect your item by ensuring that no harm...
12241 Views
A lot of us look forward to the result of moving and not the process itself. It is pretty typical behavior, though. As modern people, many things...
11129 Views
Moving from one state, city, or even to a whole different county, is something that is either dictated by choice or circumstance. This is because,...
11129 Views
Building a custom home is an exciting adventure. It’s your chance to bring your vision to life and create an area that sincerely displays...
10911 Views
Statistics
| Members | |
|---|---|
| Members: | 16703 |
| Publishing | |
|---|---|
| Articles: | 78,338 |
| Categories: | 202 |
| Online | |
|---|---|
| Active Users: | 186 |
| Members: | 12 |
| Guests: | 174 |
| Bots: | 3688 |
| Visits last 24h (live): | 1268 |
| Visits last 24h (bots): | 41596 |