Magento 2: save product review

Viewed 1147

I am working on a Magento 2 extension that will send a message to me when a new product review is written. I've tried to create an observer, but it never seems to work.

In ets/events.xml, I have this:

<event name="review_save_after">
    <observer 
        name = "jeroen_update_product_review"
        instance = "Jeroen\ReviewIntegration\Observer\ProductReview" />
</event>

In Jeroen\ReviewIntegration\Observer\ProductReview:

namespace Jeroen\ReviewIntegration\Observer;
use Magento\Framework\Event\ObserverInterface;

class ProductReview implements ObserverInterface
{
     protected $_storeManager;
     protected $_request;

     public function __construct(
         \Magento\Store\Model\StoreManagerInterface $storeManager,
         \Magento\Framework\App\Request\Http $request
     ) {
         $this->_storeManager = $storeManager;
         $this->_request = $request;
     }

     public function execute(\Magento\Framework\Event\Observer $observer)
     {
          return 'test';
     }
}

This always gives a a blank page after a new review is written (and after the status of the review is updated). Can anyone find out what I am doing wrong?

3 Answers

We can use Plugin to achieve any functionality after review save

di.xml file

<type name="Magento\Review\Controller\Product\Post">
    <plugin name="After_save_product_review"
                type="Module\Custom\Plugin\UpdateReviewSaveAfter" />
</type>

Plugin file

namespace Module\Custom\Plugin;

class UpdateReviewSaveAfter
{
    public function afterExecute(
        \Magento\Review\Controller\Product\Post $subject,
        $result)
    {       
        //your fuctionality

        return $result;
    }
}
Related