Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2 Add Product Review And Rating Programatically

In this post i am going to explain that how to create product review and rating programatically from csv file in Magento 2.

Magento 2 Add Product Review And Rating Programatically


Create import_review.php file in root of your magento and add following code.

<?php

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

ini_set('display_errors', 1);

$file = fopen('products_review.csv', 'r');


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewFactory = $objectManager->get("Magento\Review\Model\ReviewFactory");
$ratingFactory = $objectManager->get("Magento\Review\Model\RatingFactory");
$storeManager = $objectManager->get("Magento\Store\Model\StoreManagerInterface");

$i = 0;
while (($line = fgetcsv($file)) !== FALSE) {
    if (count($line) > 0) {

        $sku = $line[0];
        $review_rating = $line[1];
        $reviewTitle = $line[2];
        $reviewDetail = $line[3];
        $UserEmail = $line[4];
        $customerId = 0;

        if ($i > 0) {
            $productId = $objectManager->get('Magento\Catalog\Model\Product')->getIdBySku($sku);

            if ($productId) {
                $reviewFinalData = array();
                $reviewFinalData['ratings'][1] = $review_rating;
                $reviewFinalData['ratings'][2] = $review_rating;
                $reviewFinalData['ratings'][3] = $review_rating;

                $reviewFinalData['nickname'] = "Guest";

                if ($UserEmail) {
                    $websiteId = $storeManager->getWebsite()->getWebsiteId();
                    $customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
                    $customer = $customerFactory->create();
                    $customer->setWebsiteId($websiteId);
                    $customer->loadByEmail($UserEmail);
                    $data = $customer->getData();
                    $customerId = $data['entity_id'];
                    $firstname = $data['firstname'];
                    $lastname = ($data['lastname'] ? " " . $data['lastname'] : "");
                    $reviewFinalData['nickname'] = $firstname . $lastname;
                }

                $reviewFinalData['title'] = $reviewTitle;
                $reviewFinalData['detail'] = $reviewDetail;

                $review = $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($storeManager->getStore()->getId())
                        ->setStores([$storeManager->getStore()->getId()]);

                if ($customerId) {
                    $review->setCustomerId($customerId);
                }

                $review->save();

                foreach ($reviewFinalData['ratings'] as $ratingId => $optionId) {
                    $ratingFactory->create()
                            ->setRatingId($ratingId)
                            ->setReviewId($review->getId())
                            ->addOptionVote($optionId, $productId);
                }

                $review->aggregate();
            }
        }
    }
    $i++;
}
?>


You can download sample csv file and script from following link

Download

Please support us, Like us on Facebook.

0 comments:

Post a Comment

 

Copyright @ 2017 HKBlog.