This code is for import reviews from the other database table into magento2 product review.
Step1- Please empty review and rating tables.
Magento 2 Database table list.
Step2- Create 2 tables in your magento database test_customer and test_reviews.
CREATE TABLE `test_customer` (
`customerid` int(11) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`emailaddress` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `test_reviews` (
`id` int(11) NOT NULL,
`lastmodified` varchar(255) NOT NULL,
`productcode` varchar(255) NOT NULL,
`reviewtitle` varchar(255) NOT NULL,
`rate` varchar(255) NOT NULL,
`customerid` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`active` varchar(255) NOT NULL,
`reviewdescription` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Here you can insert the data from your csv's or other database
Step3- create a php page and paste the below code.
<?php
error_reporting(E_ALL);
set_time_limit(0);
ini_set('display_errors', '1');
ini_set('max_execution_time', 0);
ini_set('memory_limit', '3072M');
ini_set('auto_detect_line_endings', TRUE);
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');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
//Get Customer email for oldsite
function get_customer_email($id)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$sql = "Select * FROM test_customer_au where customerid=".$id;
$result = $connection->fetchAll($sql);
if(count($result)>0){
$email= $result[0]['emailaddress'];
}
else{$email= 'abc@testwebsite.com';}
return $email;
}
// start code where old site review
/*
* Select * FROM test_reviews where 1
* I got all the reivew by this query.
*
*
* */
$sql = "Select * FROM test_reviews where 1 ";
$result = $connection->fetchAll($sql);
foreach($result as $kk){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$customerId = '1234'; // default userid in my case when user is not avalable other wise blank
$nickname= $kk['name'].', '.$kk['location'];
$timestamp = '';
$new_date_format = '';
$timestamp = strtotime($kk['lastmodified']);
$new_date_format = date('Y-m-d H:i:s', $timestamp);
// Load product data
$sku =$kk['productcode'];
$productObject = $objectManager->get('Magento\Catalog\Model\Product');
$product = $productObject->loadByAttribute('sku', $sku);
$productId = $product->getId();
// Load customer data
/*
* I load email id from here by the mapping of ID from the magento2 and custom table using function
* get_customer_email()
*
* */
if($kk['customerid']!=""){
$email = get_customer_email($kk['customerid']);
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
$collection = $customerObj->addAttributeToSelect('*')
->addAttributeToFilter('email',$email)
->load();
$cdata=$collection->getData();
if(isset($cdata[0]['entity_id']) && $cdata[0]['entity_id']!=''){$customerId = $cdata[0]['entity_id'];}else{$customerId = '4611';}
}
// Save Reviews
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
if($kk['active']=="Y"){
$_review = $objectManager->get("Magento\Review\Model\Review")
->setEntityPkValue($productId)//product Id
->setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED)// approved /PENDING
->setTitle($kk['reviewtitle'])
->setDetail($kk['reviewdescription'])
->setEntityId(1)
->setStoreId(2)
->setStores(2)
->setCustomerId($customerId)//get dynamically here
->setNickname($nickname)
->save();
}
if($kk['active']=="N"){
$_review = $objectManager->get("Magento\Review\Model\Review")
->setEntityPkValue($productId)//product Id
->setStatusId(\Magento\Review\Model\Review::STATUS_PENDING)// approved /PENDING
->setTitle($kk['reviewtitle'])
->setDetail($kk['reviewdescription'])
->setEntityId(1)
->setStoreId(2)
->setStores(2)
->setCustomerId($customerId)//get dynamically here
->setNickname($nickname)
->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 ='';
if($kk['rate'] == "1")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '16',
);
}
if($kk['rate'] == "2")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '17',
);
}
if($kk['rate'] == "3")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '18',
);
}
if($kk['rate'] == "4")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '19',
);
}
if($kk['rate'] == "5")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '20',
);
}
foreach ($ratingOptions as $ratingId => $optionIds) {
$objectManager->get("Magento\Review\Model\Rating")
->setRatingId($ratingId)
->setReviewId($_review->getId())
->addOptionVote($optionIds, $productId);
}
// generates summary
$_review->aggregate();
// if you want to save review in previous dates
$_review->setCreatedAt($new_date_format);
$_review->save();
echo "<br/>Rating has been saved success !!!!!!!!!= ".$_review->getId();
}
?>
Step1- Please empty review and rating tables.
Magento 2 Database table list.
- review
- review_detail
- review_entity_summary
- review_store
- rating_option_vote
- rating_option_vote_aggregated
Step2- Create 2 tables in your magento database test_customer and test_reviews.
CREATE TABLE `test_customer` (
`customerid` int(11) NOT NULL,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) NOT NULL,
`emailaddress` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE `test_reviews` (
`id` int(11) NOT NULL,
`lastmodified` varchar(255) NOT NULL,
`productcode` varchar(255) NOT NULL,
`reviewtitle` varchar(255) NOT NULL,
`rate` varchar(255) NOT NULL,
`customerid` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`location` varchar(255) NOT NULL,
`active` varchar(255) NOT NULL,
`reviewdescription` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Here you can insert the data from your csv's or other database
Step3- create a php page and paste the below code.
<?php
error_reporting(E_ALL);
set_time_limit(0);
ini_set('display_errors', '1');
ini_set('max_execution_time', 0);
ini_set('memory_limit', '3072M');
ini_set('auto_detect_line_endings', TRUE);
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');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
//Get Customer email for oldsite
function get_customer_email($id)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$sql = "Select * FROM test_customer_au where customerid=".$id;
$result = $connection->fetchAll($sql);
if(count($result)>0){
$email= $result[0]['emailaddress'];
}
else{$email= 'abc@testwebsite.com';}
return $email;
}
// start code where old site review
/*
* Select * FROM test_reviews where 1
* I got all the reivew by this query.
*
*
* */
$sql = "Select * FROM test_reviews where 1 ";
$result = $connection->fetchAll($sql);
foreach($result as $kk){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
$customerId = '1234'; // default userid in my case when user is not avalable other wise blank
$nickname= $kk['name'].', '.$kk['location'];
$timestamp = '';
$new_date_format = '';
$timestamp = strtotime($kk['lastmodified']);
$new_date_format = date('Y-m-d H:i:s', $timestamp);
// Load product data
$sku =$kk['productcode'];
$productObject = $objectManager->get('Magento\Catalog\Model\Product');
$product = $productObject->loadByAttribute('sku', $sku);
$productId = $product->getId();
// Load customer data
/*
* I load email id from here by the mapping of ID from the magento2 and custom table using function
* get_customer_email()
*
* */
if($kk['customerid']!=""){
$email = get_customer_email($kk['customerid']);
$customerObj = $objectManager->create('Magento\Customer\Model\ResourceModel\Customer\Collection');
$collection = $customerObj->addAttributeToSelect('*')
->addAttributeToFilter('email',$email)
->load();
$cdata=$collection->getData();
if(isset($cdata[0]['entity_id']) && $cdata[0]['entity_id']!=''){$customerId = $cdata[0]['entity_id'];}else{$customerId = '4611';}
}
// Save Reviews
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
if($kk['active']=="Y"){
$_review = $objectManager->get("Magento\Review\Model\Review")
->setEntityPkValue($productId)//product Id
->setStatusId(\Magento\Review\Model\Review::STATUS_APPROVED)// approved /PENDING
->setTitle($kk['reviewtitle'])
->setDetail($kk['reviewdescription'])
->setEntityId(1)
->setStoreId(2)
->setStores(2)
->setCustomerId($customerId)//get dynamically here
->setNickname($nickname)
->save();
}
if($kk['active']=="N"){
$_review = $objectManager->get("Magento\Review\Model\Review")
->setEntityPkValue($productId)//product Id
->setStatusId(\Magento\Review\Model\Review::STATUS_PENDING)// approved /PENDING
->setTitle($kk['reviewtitle'])
->setDetail($kk['reviewdescription'])
->setEntityId(1)
->setStoreId(2)
->setStores(2)
->setCustomerId($customerId)//get dynamically here
->setNickname($nickname)
->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 ='';
if($kk['rate'] == "1")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '16',
);
}
if($kk['rate'] == "2")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '17',
);
}
if($kk['rate'] == "3")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '18',
);
}
if($kk['rate'] == "4")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '19',
);
}
if($kk['rate'] == "5")
{
$ratingOptions = array(
// '1' => '1',
// '2' => '7',
// '3' => '13',
'4' => '20',
);
}
foreach ($ratingOptions as $ratingId => $optionIds) {
$objectManager->get("Magento\Review\Model\Rating")
->setRatingId($ratingId)
->setReviewId($_review->getId())
->addOptionVote($optionIds, $productId);
}
// generates summary
$_review->aggregate();
// if you want to save review in previous dates
$_review->setCreatedAt($new_date_format);
$_review->save();
echo "<br/>Rating has been saved success !!!!!!!!!= ".$_review->getId();
}
?>