In my magento, there are appox. 10K product available. I want to add reviews and ratings in all products.

If I do this from admin, It will take lots of time.

I want to add this via script or programmatically.

Can anyone please help me how to do this?


1 Answer 1


create a controller Index.php on this below path and run this controller at front side :



namespace CompanyName\ModuleName\Controller\Review;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
    protected $_productCollectionFactory;
    public function __construct(
        Context $context,
        \Magento\Review\Model\ReviewFactory $reviewFactory,
        \Magento\Review\Model\RatingFactory $ratingFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_reviewFactory = $reviewFactory;
        $this->_ratingFactory = $ratingFactory;
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_storeManager = $storeManager;

    public function execute()
        $collection = $this->_productCollectionFactory->create()->addAttributeToSelect('*');
        foreach ($collection as $key => $value) {
            $productId = $value->getId();
            $reviewFinalData['ratings'][1] = 5;
            $reviewFinalData['ratings'][2] = 5;
            $reviewFinalData['ratings'][3] = 5;
            $reviewFinalData['nickname'] = "Rohan Hapani"; //add user nickname
            $reviewFinalData['title'] = "Dynamic Review Title"; //add title of the review
            $reviewFinalData['detail'] = "Magento 2 Product Review Script"; //add details of the review
            $review = $this->_reviewFactory->create()->setData($reviewFinalData);
                ->setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED) //By default set approved

            foreach ($reviewFinalData['ratings'] as $ratingId => $optionId) {
                    ->addOptionVote($optionId, $productId);
  • Yes, Working fine thank u. Commented Jul 30, 2020 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.