0

I am trying to programmatically create a product review for an existing product in Magento 2.

4 Answers 4

6

Note: Here i am giving u sample example on creating review and rating programmatically by using test script running at root folder of magento2

<?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; //for Guest user $customerId=Null;
$customerNickName='STACK EXCHANGE';
$reviewTitle='STACK EXCHANGE';
$reviewDetail='STACK EXCHANGE';
$StoreId=1;
$title='STACK EXCHANGE';


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


echo "Review Has been saved ";

echo "/////FOR SAVING RATING /////////
     ///////////////////////////////";

/* 
 $_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
);*/

//Lets Assume User Chooses Rating based on Rating Attributes called(quality,value,price,rating)
$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);

}
echo  "Latest REVIEW ID ===".$_review->getId()."</br>";     
$_review->aggregate();
echo "Rating has been saved submitted  successfully";

?>
8
  • Can you please tell me how I store it for guest user? May 1, 2018 at 10:25
  • Hi @Mayur just keep $customerId=Null for guest user. May 2, 2018 at 4:57
  • you will get output as shown in this screenshot prnt.sc/jcmfm5 as well i updated code please check the same May 2, 2018 at 5:00
  • review added and i can see it in admin but in frontend its not showing any idea about that. May 23, 2018 at 6:56
  • please approve the review from admin end check the same in the frontend and let me know May 23, 2018 at 7:05
0

You can browse the code of Magento\Review\Controller\Product\Post.php to see how post data is handled to create a review record.

0

Here is my work for posting approved reviews SQL only:

https://github.com/rajan-kz/Magento-2-SQL-reviews

Hope this was useful.

0

HELLO STACK EXCHANGEPHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (estorexl_eu_2021_staging.review_detail, CONSTRAINT REVIEW_DETAIL_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID FOREIGN KEY (customer_id) REFERENCES customer_entity (entity_id) ON DELETE SE) in /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91 Stack trace: #0 /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(91): PDOStatement->execute() #1 /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(107): Magento\Framework\DB\Statement\Pdo\Mysql->Magento\Framework\DB\Statement\Pdo{closure}() #2 /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php(92): Magento\Framework\DB\Statement\Pdo\Mysql->tryExecute() #3 /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/zend in /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php on line 110 Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (estorexl_eu_2021_staging.review_detail, CONSTRAINT REVIEW_DETAIL_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID FOREIGN KEY (customer_id) REFERENCES customer_entity (entity_id) ON DELETE SE) in /home/httpd/ebouwmarkt/ebouwmarkt.nl/public_html/vendor/magento/framework/DB/Statement/Pdo/Mysql.php:91 Stack trace:

1
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review
    – sv3n
    Oct 16, 2021 at 22:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.