In Magento 2, the order confirmation email is typically sent immediately after an order is placed. However, some businesses may prefer not to send this email if the order status is “Pending.” This ensures that the customer receives the confirmation only when the order is actually being processed and not just pending payment. For instance, when placing an order using a credit card or other online payment methods, the initial order status may be “Pending” until the payment is confirmed. In such cases, we need to prevent the confirmation email from being sent to the customer while the order is still in the “Pending” state.
Implementing this will ensure that customers are notified only when their orders are actively being processed, reducing confusion and improving communication.
Here’s a step-by-step guide on how to send the order confirmation email once the order status changes to any status except Pending and prevent the email from being sent when the order status is “Pending.” In this blog post I will explain How to Stop Order Confirmation Email on Order Status Pending in Magento 2.
Stop the Order Confirmation email being sent to customer on Pending order status
Here we will create a around plugin to to stop the confirmation email to customer on pending order status. You can more lear about plugin here.
Step 1:
Create di.xml file at app/code/Vendor/Module/etc directory and place the below code
<?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\Sender\OrderSender">
<plugin name="order_sender_plugin" type="Vendor\Module\Plugin\OrderSenderPlugin" />
</type>
</config>
Step 2:
Create OrderSenderPlugin.php file at app/code/Vendor/Module/Plugin directory and place the below code:
<?php
namespace Vendor\Module\Plugin;
use Magento\Sales\Model\Order;
class OrderSenderPlugin
{
public function aroundSend(
\Magento\Sales\Model\Order\Email\Sender\OrderSender $subject,
\Closure $proceed,
Order $order,
$forceSyncMode = false
) {
if ($order->getStatus() == 'pending') {
return false;
}
$result = $proceed($order, $forceSyncMode);
return $result;
}
}
Above two steps will stop the order confirmation email to the customer if order status is Pending.
Now we will resend the order confirmation email to the customer upon order save if the order status is not ‘Pending.
Resend the order confirmation email to the customer upon order save if the order status is not ‘Pending.
Step 3:
Create events.xml file at app/code/Vendor/Module/etc directory and place the below code:
<?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="sales_order_save_after">
<observer name="resend_order_email" instance="Vendor\Module\Observer\ResendOrderEmail" />
</event>
</config>
Step 4:
Create ResendOrderEmail.php file at app/code/Vendor/Module/Observer directory and place the code below:
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order;
class ResendOrderEmail implements ObserverInterface
{
protected $orderSender;
public function __construct(OrderSender $orderSender)
{
$this->orderSender = $orderSender;
}
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
if ($order->getStatus() != 'pending' && !$order->getEmailSent()) {
try {
$this->orderSender->send($order, true);
$order->setEmailSent(true);
$order->save();
} catch (\Exception $e) {
}
}
}
}
These two steps 3 and 4 will resend the order confirmation email on order status change except Pending order status.
This observer will not process the order if the confirmation email has already been sent, as we have used the email_sent flag in the order object.
If we send the confirmation email, we will set this flag to true, so if the order status changes again later, the email will not be resent.
You may also like this:
Conclusion:
By customizing Magento 2 to stop the order confirmation email when the order status is “Pending” and resume sending it when the status changes to “Processing” or other valid states, This approach is especially useful when dealing with payment methods that may take time to verify, such as credit cards or other online payment gateways.
With the implementation of this observer, customers are notified only when their orders are actively being processed, reducing confusion and creating a more streamlined order experience. Additionally, by utilizing the email_sent flag, we prevent duplicate emails from being sent when the order status changes multiple times.
This customization not only enhances customer satisfaction but also ensures that your store’s email workflow aligns with your specific business processes.
Hello,
I particularly appreciate how you’ve outlined the exact steps in the code and explained the logic behind it. For those who might be new to Magento 2’s event-driven architecture.
Looking forward to more insightful Magento 2 tips from you!