Send New Product Review Notification Email

January 13, 2015  |  1 Comments  |  by Raj (MagePsycho)  |  Latest, Magento

This is probably a feature which store owner might be looking for.
As of now store owner has to check for new product reviews from Magento backend in frequent manner.

Won’t it be better if admin gets a instant email whenever a new product review is posted? So that whatever action needs to be carried out with the pending review can be done quickly. You don’t need to buy expensive extension in order to get job done. Here I will be sharing a simple trick to achieve this.

1. Crate a skeleton module(for example: MagePsycho_Reviewnotifier) and register the event: review_save_after
File: app/code/local/MagePsycho/Reviewnotifier/etc/config.xml

<frontend>
    ...
    <events>
        <review_save_after>
            <observers>
                <magePsycho_reviewnotifier_review_save_after>
                    <type>singleton</type>
                    <class>magePsycho_reviewnotifier/observer</class>
                    <method>reviewSaveAfter</method>
                </magePsycho_reviewnotifier_review_save_after>
            </observers>
        </review_save_after>
    </events>
    ...
</frontend>

2. Implement the observer model
File: app/code/local/MagePsycho/Reviewnotifier/Model/Observer.php

<?php

/**
 * Observer Model for Review Notifier
 * 
 * @author MagePsycho<magepsycho@gmail.com>
 * @package MagePsycho_Reviewnotifier
 * Class MagePsycho_Reviewnotifier_Model_Observer
 */
class MagePsycho_Reviewnotifier_Model_Observer
{
    /**
     * Send notification email when product review is posted
     *
     * @param Varien_Event_Observer $observer
     */
    public function reviewSaveAfter(Varien_Event_Observer $observer)
    {
        $review = $observer->object;
        if ($review) {
            $emails     = array('info@yourstore.com', 'email1@yourstore.com'); #Edit admin emails
            try {
                //@todo - use configurable email templates
                $this->_sendNotificationEmail($emails, $review);
            } catch (Exception $e) {
                Mage::logException($e);
            }
        } else {
            Mage::log('ERROR::UNABLE TO LOAD REVIEW');
        }
    }

    protected function _sendNotificationEmail($emails, $review)
    {
        if (count($emails)) {
            $product        = Mage::getModel('catalog/product')->load($review->getEntityPkValue());
            $starRatings    = array_values($review->getRatings());

            $body = 'New product review has been posted!<br /><br />';
            $body .= 'Customer Name: ' . $review->getNickname() . '<br />';
            $body .= 'Product: ' . sprintf('<a href="%s" target="_blank">%s</a>', $product->getProductUrl(), $product->getName()) . '<br />';
            $body .= 'Star Rating: ' . (isset($starRatings[0]) ? $starRatings[0] : 'N/A') . '<br />';
            $body .= 'Review Title: ' . $review->getTitle() . '<br />';
            $body .= 'Review Description: ' . $review->getDetail() . '<br />';

            foreach ($emails as $toEmail) {
                $mail = Mage::getModel('core/email');
                $mail->setToName('YourStore Admin');
                $mail->setToEmail($toEmail);
                $mail->setBody($body);
                $mail->setSubject('YourStore: New Product Review');
                $mail->setFromEmail('donotreply@yourstore.com');
                $mail->setFromName("YourStore");
                $mail->setType('html');

                try {
                    $mail->send();
                }
                catch (Exception $e) {
                    Mage::logException($e);
                }
            }
        }
    }
}

Please leave your comments if it works for you or if you have any queries.