We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How to Auto Approve Product Review For Registered Customers in Magento 2
Follow the steps to auto approve the product review for registered customers in magento 2
Create registration.php
Directory
app\code\VendorName\ModuleName\registration.php
Content of registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'VendorName_ModuleName',
__DIR__
);
Create module.xml
Directory
app\code\VendorName\ModuleName\etc\module.xml
Content of module.xml
<?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="VendorName_ModuleName" setup_version="1.0.0"/>
</config>
Create events.xml
Directory
app\code\VendorName\ModuleName\etc\frontend\events.xml
Content of events.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="review_save_before">
<observer name="auto_approve_review" instance="VendorName\ModuleName\Observer\ReviewApproveBefore"/>
</event>
</config>
Create email_templates.xml
Directory
app\code\VendorName\ModuleName\etc\email_templates.xml
Content of email_templates.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
<template id="review_template" label="Auto Approve Review email" file="Demo/review.html" type="html" module="VendorName_ModuleName" area="frontend"/>
</config>
Create review.html
Directory
app\code\VendorName\ModuleName\view\frontend\email\demo\review.html
Content of review.html
<!--@subject Thank You For Your Review @-->
<!--@vars {
"var var1":"Customer Name"
} @-->
{Error in template processing}
<!-- here $var1 , $var2, are variables in which we
assign values when we use this template for send mail-->
<!-- you can Modify content of template according to your requirement-->
<table>
<tr class="email-intro">
<td>
<p class="greeting">,</p>
<p>
Your custom message.
</p>
</td>
</tr>
</table>
{Error in template processing}
Create ReviewApproveBefore.php
Directory
app\code\VendorName\ModuleName\Observer\ReviewApproveBefore.php
Content of ReviewApproveBefore.php
<?php
namespace VendorName\ModuleName\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Review\Model\Review;
class ReviewApproveBefore implements ObserverInterface
{
private $customerSession;
private $request;
private $transportBuilder;
private $storeManager;
public function __construct(
\Magento\Customer\Model\Session $customerSession,
\Magento\Framework\App\Request\Http $request,
\Magento\Framework\Mail\Template\TransportBuilder $transportBuilder,
\Magento\Store\Model\StoreManagerInterface $storeManager
){
$this->customerSession = $customerSession;
$this->request = $request;
$this->transportBuilder = $transportBuilder;
$this->storeManager = $storeManager;
}
public function execute(Observer $observer)
{
if($this->customerSession->isLoggedIn()) {
$review = $observer->getDataByKey('object');
$review->setStatusId(Review::STATUS_APPROVED); // set status approve
try {
$store = $this->storeManager->getStore()->getId();
$transport = $this->transportBuilder->setTemplateIdentifier('meetanshi_review_template') // get template id from email_template.xml
->setTemplateOptions(['area' => 'frontend', 'store' => $store])
->setTemplateVars(
[
'store' => $this->storeManager->getStore(),
'var1' => $this->customerSession->getCustomerData()->getFirstname(), //var1 variable used in review.html
]
)
->setFrom('general')
// you can config general email address in Store -> Configuration -> General -> Store Email Addresses
->addTo($this->customerSession->getCustomerData()->getEmail(), $this->customerSession->getCustomerData()->getFirstname())
->getTransport();
$transport->sendMessage();
}catch (\Exception $e){
}
}
}
}
If you want any kind of help please visit our Magento 2 Development Service and get a Quote.