Magento2 Product Review Ratings – How to Create Rating Programmatically

magento2productratingsreview

I am able to create a review but unable to create rating.
What exactly data should consider here please suggest me about rating options $XXXXXXX & $YYYYYYY ?

ReferenceLink 1

Reference Link 2

<?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); 
echo "HELLO STACK EXCHANGE";

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_review = $objectManager->get("Magento\Review\Model\Review")
    ->setEntityPkValue(2)//product Id
    ->setStatusId(\Magento\Review\Model\Review::STATUS_PENDING)// approved
    ->setTitle('STACK EXCHANGE')
    ->setDetail('STACK EXCHANGE')
    ->setEntityId(1)
    ->setStoreId(1)
    ->setStores(1)
    ->setCustomerId(1)//get dynamically here 
    ->setNickname('STACK EXCHANGE')
    ->save();

echo "review has been saved..";

$_rating = $objectManager->get("Magento\Review\Model\Rating")
    ->setRatingId($XXXXXXX)
    ->setReviewId($_review->getId())
    ->addOptionVote($YYYYYYY,2);
$_review->aggregate();

echo "Rating has been saved success !!!!!!!!!";

?>

Best Answer

Finally I got the output:

<?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);
echo "HELLO STACK EXCHANGE";


$productId=5;
$customerId=13;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_review = $objectManager->get("Magento\Review\Model\Review")
    ->setEntityPkValue($productId)//product Id
    ->setStatusId(\Magento\Review\Model\Review::STATUS_PENDING)// approved
    ->setTitle('STACK EXCHANGE')
    ->setDetail('STACK EXCHANGE')
    ->setEntityId(1)
    ->setStoreId(1)
    ->setStores(1)
    ->setCustomerId($customerId)//get dynamically here
    ->setNickname('STACK EXCHANGE')
    ->save();


echo "Review Has been saved ";

/*
$_ratingOptions = array(
    1 => array(1 => 1,  2 => 2,  3 => 3,  4 => 4,  5 => 5), //quality
    2 => array(1 => 6,  2 => 7,  3 => 8,  4 => 9,  5 => 10),//value
    3 => array(1 => 11, 2 => 12, 3 => 13, 4 => 14, 5 => 15),//price
    4 => array(1 => 16, 2 => 17, 3 => 18, 4 => 19, 5 => 20) //rating
);
*/

//just Assume user selected rating options

$ratingOptions = array(
    '1' => '1',
    '2' => '7',
    '3' => '13',
    '4' => '19',
);

foreach ($ratingOptions as $ratingId => $optionIds) {
    $objectManager->get("Magento\Review\Model\Rating")
        ->setRatingId($ratingId)
        ->setReviewId($_review->getId())
        ->addOptionVote($optionIds, $productId);
}

$_review->aggregate();

echo "Rating has been saved success !!!!!!!!!";
?>
Related Topic