How To

How to Add Product Review on Product Detail Page Programmatically in Magento 2

Hello Magento Friends,

In this Magento blog, I will explain How to Add Product Reviews on the Product Detail Page Programmatically in Magento 2.

Product reviews and ratings are a great way to let satisfied customers leave their thoughts for your business. Displaying those product reviews on your product detail page helps other customers to make a purchasing decision. To display reviews on the storefront, you need to encourage existing customers to leave a review for their purchase. Integrate Review Reminder Extension for Magento 2 to recall customers for their opinion. Also, allow guest users to write reviews to your Magento 2 store.

In Magento 2 you can add reviews to the product detail page using 2 methods:

  1. Add Review from Admin Panel
  2. Add Review Programmatically

Today we will learn the second method – Add Product Review Programmatically in Magento 2. Let’s get started

Steps to Add Product Review on Product Detail Page Programmatically in Magento 2:

Step 1: Add the routes.xml file to the below path

app/code/Vendor/Extension/etc/frontend/routes.xml

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="addreview" frontName="addreview">
            <module name="Vendor_Extension" />
        </route>
    </router>
</config>

Step 2: Add the below PHP file to your module.

app/code/Vendor/Extension/Controller/Index/Index.php

Then add the code given below

<?php
namespace Vendor\Extension\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Review\Model\ReviewFactory;
use Magento\Review\Model\RatingFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Review\Model\Review;

class Index extends Action
{
    protected $reviewFactory;
    protected $storeManager;

    public function __construct(
        Context $context,
        ReviewFactory $reviewFactory,
        RatingFactory $ratingFactory,
        StoreManagerInterface $storeManager)
    {
        $this->reviewFactory = $reviewFactory;
        $this->_ratingFactory = $ratingFactory;
        $this->storeManager = $storeManager;
        parent::__construct($context);
    }

    public function execute()
    {
        $productId=2; // Add productId
        $customerId=1; // Add customer Id
        $customerNickName='Customer Nickname';
        $reviewTitle='Test Review';
        $reviewDetail='Add Review programmatically';
        $storeId=1;

        $_review = $this->reviewFactory->create()
        ->setEntityPkValue($productId)
        ->setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED) //this will be set to approved 
        ->setTitle($reviewTitle)
        ->setDetail($reviewDetail)
        ->setEntityId(1)
        ->setStoreId($storeId)
        ->setStores(1)
        ->setCustomerId($customerId)//get dynamically here 
        ->setNickname($customerNickName)
        ->save();

        echo "Your Review Has been saved.";
    }
}

Step 3: Run this controller file to add the review to the product

Url : {{base_url}}/addreview/index/index

Conclusion:

Hence, using the above steps you can Add Product Review on the Product Detail Page Programmatically in Magento 2. If you have bulk reviews to be added, you can Import Export Product Reviews in Magento 2. Apart from reviews, you can also enable customers to add ratings for products of your Magento 2 store.

If you come across any error, share with me by commenting here and I will help you out.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago