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:
- Add Review from Admin Panel
- Add Review Programmatically
Today we will learn the second method – Add Product Review Programmatically in Magento 2. Let’s get started
Contents
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
1 2 3 4 5 6 7 8 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?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
1 |
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!