Solving “Customer Not Marked as Notified” Issue in Magento
As an experienced Magento developer, I often encounter situations where custom batch processes fail to mark customers as notified even after sending order status update emails programmatically. This issue can lead to confusion for both the customer and your support team.
Let’s dive deeper into the problem described and find a solution that ensures accurate record keeping in your Magento system.
Understanding the Current Code
The provided code seems to follow Magento’s recommended approach for updating an order’s status, adding a comment, saving changes, and sending an email notification:
$order->setIsCustomerNotified(false);
$order->setStatus("ready_for_pickup");
$order->addStatusHistoryComment($comment, false);
$order->save();
$order->sendOrderUpdateEmail($notify = true, $comment);
However, despite these steps, the customer is not being marked as notified in the order’s comment history.
Investigating the Root Cause
The issue lies in the setIsCustomerNotified(false) call. This line tells Magento that the customer should not be notified about this status change. Therefore, even though you’re sending an email using $order->sendOrderUpdateEmail(), it isn’t being tracked as a customer notification.
To resolve this, we need to ensure that the customer is marked as notified without actually sending them another email (since one has already been sent).
The Solution: Manually Marking Customer as Notified
We can manually update the is_customer_notified flag for the specific order. To do this, you’ll need to use Magento’s event system and observe the sales_order_save_after event.
Here’s a step-by-step guide:
- Create an observer class that implements
Magento\Framework\Event\ObserverInterface:
class CustomerNotificationObserver implements ObserverInterface
{
private $resourceConnection;
public function __construct(\Magento\Framework\App\ResourceConnection $resourceConnection)
{
$this->resourceConnection = $resourceConnection;
}
/**
* Update customer notification flag after order save
*
* @param \Magento\Framework\Event\Observer $observer
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getDataObject();
if ($order->getIsCustomerNotified() === false && $order->getEmailSent()) {
$this->updateNotificationFlag($order);
}
}
private function updateNotificationFlag($order)
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('sales_order');
$where = ['entity_id' => $order->getId()];
$bind = [
'is_customer_notified' => 1,
];
$connection->update($tableName, $bind, $where);
}
}
- Register the observer in your module’s
etc/events.xmlfile:
<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="customer_notification_observer" instance="Vendor\ModuleName\Observer\CustomerNotificationObserver"/>
</event>
</config>
Conclusion
By following this approach, you ensure that the customer is marked as notified in Magento’s comment history without actually sending a new email. This solution maintains accuracy in your system and prevents confusion for customers and support teams.
Remember to replace Vendor and ModuleName with your actual vendor name and module name in the observer class definition and registration.
If you’re looking for more advanced solutions or need help optimizing Magento performance, feel free to explore our services at Laravel Company.
Happy coding! Let me know if you have any further questions.