Sending Email Notifications for New Reviews in Magento
As a store owner or developer, receiving automatic notifications about new customer reviews can be incredibly valuable. It allows you to monitor your products’ performance and engage with customers more effectively.
This guide will walk you through creating a custom module that triggers an email notification whenever a new review is posted. We’ll provide a detailed outline of the process, including coding examples in Magento 2.
Understanding the Requirements
Before we dive into the code, let’s clarify what we aim to achieve:
- Automatic Trigger: The system should send an email immediately after a new review is submitted.
- Single Destination: The notification should be sent to the store’s contact email address only.
- Real-time Notification: We want this to work without relying on cron jobs for periodic checks.
Building the Custom Module
Step 1: Create the Module Structure
In your Magento project root, create a new folder named Vendor_ReviewNotification. Inside this folder, you’ll need:
/app
/code
Vendor
ReviewNotification
etc
module.xml
Controller
Index
ReviewController.php
Observer
Review
AfterSaveObserver.php
Step 2: Define the Module Configuration
The module.xml file will look something like this:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_ReviewNotification" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>
Step 3: Create the Observer
The key component is the AfterSaveObserver.php file, which will listen for the review_save_after event:
namespace Vendor\ReviewNotification\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
class AfterSaveObserver implements ObserverInterface
{
protected $scopeConfig;
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}
public function execute(Observer $observer)
{
// Retrieve the review data and send an email notification here.
// You can use Magento's built-in mailing functionality or a third-party library like PHPMailer.
}
}
Step 4: Define the Observer in the Event Configuration
In etc/events.xml, add the following observer configuration:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="review_save_after">
<observer name="review_notification_after_save_observer" instance="Vendor\ReviewNotification\Observer\AfterSaveObserver"/>
</event>
</config>
Sending the Email Notification
To send an email, you can use Magento’s built-in Magento\Framework\Mail\Template\TransportBuilder service. Inject this service into your observer and construct the mail message:
public function execute(Observer $observer)
{
$transport = $this->transportBuilder->setTemplateIdentifier('review_notification_email')
->setTemplateOptions(['area' => 'frontend', 'store_id' => 0])
->addTo($this->scopeConfig->getValue('trans_email/ident_general/email'))
->getTransport();
$transport->sendMessage();
}
The setTemplateIdentifier method should point to an existing email template in your Magento installation.
Conclusion
This guide provides a comprehensive overview of creating a custom module for automatic review notifications in Magento. By following this approach, you can ensure real-time notifications without relying on cron jobs or periodic checks.
Remember to clear the cache after installing and enabling your new module. If you encounter any issues with the email configuration, please refer to the official Magento documentation for more details.
For a more in-depth look at Magento development, consider exploring our articles on Laravel Company’s blog.