How to Auto Approve Product Review For Registered Customers in Magento 2
Customer Reviews are important and these statistics from Invespcro just shows how much:
- Customers are likely to spend 31% more with a business that has “excellent” reviews.
- 72% of consumers will take action only after reading a positive review.
- 88% of consumers trust online reviews as much as personal recommendations.
Whether it is about finding a restaurant, selecting a phone, or choosing software for your business, reviews have a big role to play in the purchase decision.
For Magento 2, which is a platform for shopping variety of products, managing customer reviews can’t be just a low priority task. Unfortunately, Magento 2, by default, does not offer a system to automate the review approvals. When it comes to the product reviews by the registered customers, there is no need to look into it before making them live as they write genuinely without being bots! However, with a pile of work that admin has, approving a customer review to be displayed in the frontend, one by one, as it comes, is not feasible.
Hence, I have implemented a programmatic solution to auto approve product review for registered customers in Magento 2.
The reviews from registered customers can be considered genuine and flaunting them on the frontend only helps you leverage the below benefits:
- Helps in analyzing and understanding the customers to improve customer service.
- Credibility and social proof!
- Leave a positive impression on potential customers with excellent reviews
- Create customer loyalty
- Improve rankings
- Use this user-generated content for marketing
- Customer engagement
The solution not only auto approves registered customers’ product reviews but also automatically send a “thank you” Email with a custom message. Acknowledging the customers’ efforts immediately is only the right thing to do! Again, automating it saves admin’s time.
Easily enjoy these benefits without having to manage the customer reviews manually in your Magento 2 store with the below solution:
Steps to Auto Approve Product Review For Registered Customers in Magento 2:
- Create registration.php file at Meetanshi/CustomModule/
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'Meetanshi_CustomModule',__DIR__); - Create module.xml file at Meetanshi/CustomModule/etc
1234<?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="Meetanshi_CustomModule" setup_version="1.0.0"/></config> - Create events.xml file at Meetanshi/CustomModule/etc/frontend
123456<?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="review_auto_approve" instance="Meetanshi\CustomModule\Observer\ReviewApproveBefore"/></event></config> - Create email_templates.xml file at Meetanshi/CustomModule/etc
12345<?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="meetanshi_review_template" label="Auto Approve Review email" file="CustomModule/review.html" type="html" module="Meetanshi_CustomModule" area="frontend"/></config> - Create review.html file at Meetanshi/CustomModule/view/frontend/email/CustomModule
1234567891011121314151617181920212223<!--@subject Thank You For Your Review @--><!--@vars {"var var1":"Customer Name"} @-->{{template config_path="design/email/header_template"}}<!-- here $var1 , $var2, are variables in which weassign 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">{{trans "%customer_name," customer_name=$var1}}</p><p>{{trans "Your custom message."}}</p></td></tr></table>{{template config_path="design/email/footer_template"}} - Create ReviewApproveBefore.php file at Meetanshi/CustomModule/Observer
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152<?phpnamespace Meetanshi\CustomModule\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 approvetry {$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){}}}}
That was all about auto approving registered customers’ product reviews to be showcased in the frontend.
Customer reviews are here to stay, and the longer you wait to start leveraging them, the more you stand to lose.
Found the post helpful? Have any doubts? Feel free to write a comment below!
Thanks.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Programmatically Add Product to Cart in Magento
How to Remove Top Link in Magento 2
Next