My requirement is to send an email to admin when a customer adds a product review. For that, I want to overwrite controller Magento\Review\Controller\Product\Post.php
Can someone help me with this?
You can also rewrite functinality with plugin
1.Create di.xml file at Vendor/Module/etc and add below code
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Review\Controller\Product\Post">
<plugin disabled="false" name="Vendor_Vendor_Plugin_Magento_Review_Controller_Product_Post" sortOrder="10" type="Vendor\Module\Plugin\Magento\Review\Controller\Product\Post"/>
</type>
</config>
2.Create Post.php file at Vendor/Module/Plugin/Magento/Review/Controller/Product and add below code
<?php
namespace Vendor\Module\Plugin\Magento\Review\Controller\Product;
class Post
{
public function afterExecute(
\Magento\Review\Controller\Product\Post $subject,
$result
) {
//Your custom code to send email to customer.
}
}
Create Namespace\Module\etc\di.xml and add the code below.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Review\Controller\Product\Post" type="Namespace\Module\Controller\Product\Post" />
</config>
create Namespace\Module\Controller\Product\Post.php
namespace Namespace\Module\Controller\Product;
use Magento\Review\Controller\Product as ProductController;
use Magento\Framework\Controller\ResultFactory;
use Magento\Review\Model\Review;
class Post extends \Magento\Review\Controller\Product\Post
{
public function execute()
{
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
if (!$this->formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
$data = $this->reviewSession->getFormData(true);
if ($data) {
$rating = [];
if (isset($data['ratings']) && is_array($data['ratings'])) {
$rating = $data['ratings'];
}
} else {
$data = $this->getRequest()->getPostValue();
$rating = $this->getRequest()->getParam('ratings', []);
}
if (($product = $this->initProduct()) && !empty($data)) {
/** @var \Magento\Review\Model\Review $review */
$review = $this->reviewFactory->create()->setData($data);
$validate = $review->validate();
if ($validate === true) {
try {
$review->setEntityId($review->getEntityIdByCode(Review::ENTITY_PRODUCT_CODE))
->setEntityPkValue($product->getId())
->setStatusId(Review::STATUS_PENDING)
->setCustomerId($this->customerSession->getCustomerId())
->setStoreId($this->storeManager->getStore()->getId())
->setStores([$this->storeManager->getStore()->getId()])
->save();
foreach ($rating as $ratingId => $optionId) {
$this->ratingFactory->create()
->setRatingId($ratingId)
->setReviewId($review->getId())
->setCustomerId($this->customerSession->getCustomerId())
->addOptionVote($optionId, $product->getId());
}
$review->aggregate();
$this->messageManager->addSuccess(__('You submitted your review for moderation.Thanks'));
} catch (\Exception $e) {
$this->reviewSession->setFormData($data);
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
} else {
$this->reviewSession->setFormData($data);
if (is_array($validate)) {
foreach ($validate as $errorMessage) {
$this->messageManager->addError($errorMessage);
}
} else {
$this->messageManager->addError(__('We can\'t post your review right now.'));
}
}
}
$redirectUrl = $this->reviewSession->getRedirectUrl(true);
$resultRedirect->setUrl($redirectUrl ?: $this->_redirect->getRedirectUrl());
return $resultRedirect;
}
}
Hope this will help you. This is working code for review controller override in magento 2. Thanks.