0

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?

Thanks.

0

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

CompanyName\ModuleName\Controller\Review\Index.php

<?php

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
        ................
    ) {
        ................
        parent::__construct($context);
        $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);
            $review->unsetData('review_id');
            $review->setEntityId($review->getEntityIdByCode(\Magento\Review\Model\Review::ENTITY_PRODUCT_CODE))
                ->setEntityPkValue($productId)
                ->setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED) //By default set approved
                ->setStoreId($this->_storeManager->getStore()->getId())
                ->setStores([$this->_storeManager->getStore()->getId()])
                ->save();

            foreach ($reviewFinalData['ratings'] as $ratingId => $optionId) {
                $this->_ratingFactory->create()
                    ->setRatingId($ratingId)
                    ->setReviewId($review->getId())
                    ->addOptionVote($optionId, $productId);
            }
            $review->aggregate();
        }
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy