3

In Magento2, Whenever a new product review posted in my store, I couldn’t get any notification. I’m always navigating to the review section to check and moderate the reviews/ratings. Is there any admin configuration available to get the product reviews/ratings notification via email?

2

I haven't found such option by default in CE.

You could create a simple module that does it. Create standard module structure/files, then:

etc/events.xml

<?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="review_save_after"> 
    <observer name="Foo_ReviewNotification::review_save_after" instance="Foo\ReviewNotification\Observer\ReviewObserver" />
</event>

observer/ReviewObserver.php

<?php

namespace Foo\ReviewNotification\Observer;

class ReviewObserver implements \Magento\Framework\Event\ObserverInterface
{
  protected $_registry;
  protected $_scopeConfig;

  public function __construct(     
    \Magento\Framework\Registry $registry,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  ) {
    $this->_registry = $registry;
    $this->_scopeConfig = $scopeConfig;
  }

  public function execute(\Magento\Framework\Event\Observer $observer) {        
    $event = $observer->getEvent();

    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
    $notifyEmail = $this->_scopeConfig->getValue("trans_email/ident_general/email", $storeScope);

    $product = $this->_registry->registry("current_product");
    $name = $product->getName();
    $sku = $product->getSku();
    $emailBody = "Product name: {$name} \n SKU: {$sku} \n Review id: {$review->getId()} \n Customer id: {$review->getCustomerId()}";
    // Mail me
    $headers = "From: {$notifyEmail}\r\n" .
        "Reply-To: {$notifyEmail}\r\n";
    mail($notifyEmail,"New product review - {$name}", $emailBody, $headers);
    return;
  }
}

That's a very basic module. You can improve it if needed.

|improve this answer|||||
0

I have done the same thing.

app/code/vendor/module/etc/events.xml

<?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="review_save_after"> 
    <observer name="vendor_email_review" instance="vendor\module\Observer\ReviewObserver" />
</event>
</config>

app/code/vendor/module/Observer/ReviewObserver.php

<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Store\Model\ScopeInterface;

class ReviewObserver implements \Magento\Framework\Event\ObserverInterface
{
    protected $_registry;
    protected $_scopeConfig;
    private $_productRepository;
    protected $_reviewFactory;
    protected $_storeManager;

    public function __construct(\Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Psr\Log\LoggerInterface $logger, TransportBuilder $transportBuilder, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this->_registry = $registry;
        $this->_scopeConfig = $scopeConfig;
        $this->logger = $logger;
        $this->transportBuilder = $transportBuilder;
        $this->_storeManager = $storeManager;
        $this->_productRepository = $productRepository;
        $this->_reviewFactory = $reviewFactory;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $state = $objectManager->get('Magento\Framework\App\State');
        if ($state->getAreaCode() != 'adminhtml')
        {
            $revieww = $observer->getEvent()
                ->getDataObject();
            $reviewId = $revieww->getReviewId();
            $product = $this
                ->_registry
                ->registry("current_product");
            $name = $product->getName();
            $sku = $product->getSku();
            $productt = $this
                ->_productRepository
                ->getById($product->getId());
            $prodlink = $productt->getUrlModel()
                ->getUrl($productt);
            $productId = $product->getId();
            $rtitile = $this
                ->_reviewFactory
                ->create()
                ->load($reviewId)->getTitle();
            $rdetail = $this
                ->_reviewFactory
                ->create()
                ->load($reviewId)->getDetail();
            $rdate = $this
                ->_reviewFactory
                ->create()
                ->load($reviewId)->getCreatedAt();
            $storeEmail = $this
                ->_scopeConfig
                ->getValue('trans_email/ident_support/email', ScopeInterface::SCOPE_STORE);
            $storeName = $this
                ->_scopeConfig
                ->getValue('trans_email/ident_support/name', ScopeInterface::SCOPE_STORE);
            $receiverInfo = ['name' => $storeName, 'email' => $storeEmail];
            $customerSession = $objectManager->get('Magento\Customer\Model\Session');
            if ($customerSession->isLoggedIn())
            {
                $email = $customerSession->getCustomer()
                    ->getEmail();
                $uname = $customerSession->getCustomer()
                    ->getName();
            }
            else
            {
                $email = "Guest";
                $uname = "Guest";
            }
            $store = $this
                ->_storeManager
                ->getStore();
            $templateParams = array();
            $templateParams['review_id'] = $reviewId;
            $templateParams['product_name'] = $name;
            $templateParams['email'] = $email;
            $templateParams['store'] = $store;
            $templateParams['prodlink'] = $prodlink;
            $templateParams['uname'] = $uname;
            $templateParams['administrator_name'] = $receiverInfo['name'];
            $templateParams['sku'] = $sku;
            $templateParams['rtitle'] = $rtitile;
            $templateParams['rdetail'] = $rdetail;
            $templateParams['rdate'] = $rdate;
            $transport = $this
                ->transportBuilder
                ->setTemplateIdentifier('review_email_observer')
                ->setTemplateOptions(['area' => 'frontend', 'store' => $store->getId() ])
                ->addTo($receiverInfo['email'], $receiverInfo['name'])->setTemplateVars($templateParams)->setFrom('general')
                ->getTransport();
            try
            {
                $transport->sendMessage();
            }
            catch(\Exception $e)
            {
                $this
                    ->logger
                    ->critical($e->getMessage());
            }
        }
    }
}

app/code/vendor/module/etc/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_email_observer" label="ReviewObserver" file="reviewemail.html" type="html" module="Vendor_Module" area="frontend"/>
</config>

app/code/Vendor/Module/view/frontend/email/reviewemail.html

<!--@subject {{trans "New Review from %uname has been submitted on %product_name" product_name=$product_name  uname=$uname}} @-->
<!--@vars {
"var store.getFrontendName()":"Store Name",
"var customer.name":"Customer Name",
"var administrator_name":"Administrator Name"
} @-->

{{template config_path="design/email/header_template"}}
<p class="greeting">{{trans "Hello <b>%administrator_name</b>," administrator_name=$administrator_name|row}}</p>

<p>{{trans 'The customer named <b>%uname</b> just submitted a review on  %store_name:' uname=$uname store_name=$store.getBaseUrl()|row}}</p>
<table style="width: 750px;border-collapse: collapse;margin:50px auto;" border="1px">
  <tr style="background: #eee;">
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Review ID</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%review_id</td>" review_id=$review_id|row}}
  </tr>
  <tr>
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>SKU</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%sku</td>" sku=$sku|row}}
  </tr>
  <tr style="background: #eee;">
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Product</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><a href='%prodlink'>%product_name</a></td>" product_name=$product_name prodlink=$prodlink|row}}
  </tr>
  <tr>
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Review Summary</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%rtitle</td>" rtitle=$rtitle|row}}
  </tr>
  <tr style="background: #eee;">
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Review Detail</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><q><i>%rdetail</i></q></td>" rdetail=$rdetail|row}}
  </tr>
  <tr>
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Customer Name<b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%uname</td>" uname=$uname|row}}
  </tr>
  <tr style="background: #eee;">
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Customer Email</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%email</td>" email=$email|row}}
  </tr>
  <tr>
    <td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'><b>Date & Time</b></td>
    {{trans "<td style='padding: 10px;border: 1px solid #ccc;text-align: left;font-size: 18px;'>%rdate</td>" rdate=$rdate|row}}
  </tr>
</table>
{{template config_path="design/email/footer_template"}}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.