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

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

46 mins ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

2 hours ago

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

2 days ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

5 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 week ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 week ago