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 ?
<?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: