Magento Import product reviews from a csvImage Update via CSV importMagento Import Product With image in CSVUnable to import more csv files into Magentoimport products from csvthe correct syntax for CSV Customer IMPORT? (from 1.3.1 to 1.9.1)CSV Import: editing configurable childs not workingCSV import not updating attributesImport Product Data w/ CSV from command line (not dataflow)programatically import csv - magento 2Import Product from other stores

Did Pope Urban II issue the papal bull "terra nullius" in 1095?

Is it OK to draw different current from L1 and L2 on NEMA 14-50?

Do you "gain" 1st level?

Help, I cannot decide when to start the story

Bringing Power Supplies on Plane?

How far did Gandalf and the Balrog drop from the bridge in Moria?

Scam? Phone call from "Department of Social Security" asking me to call back

Are employers legally allowed to pay employees in goods and services equal to or greater than the minimum wage?

What can Amex do if I cancel their card after using the sign up bonus miles?

"Mouth-breathing" as slang for stupidity

Good textbook for queueing theory and performance modeling

Causal Diagrams using Wolfram?

Attacking the Hydra

Installing Windows to flash UEFI/ BIOS, then reinstalling Ubuntu

What is the farthest a camera can see?

How to gracefully leave a company you helped start?

Does fossil fuels use since 1990 account for half of all the fossil fuels used in history?

graphs in latex

Link for download latest Edubuntu

How was the murder committed?

How can God warn people of the upcoming rapture without disrupting society?

How do I ask for 2-3 days per week remote work in a job interview?

Why aren’t there water shutoff valves for each room?

How do I call a 6-digit Australian phone number with a US-based mobile phone?



Magento Import product reviews from a csv


Image Update via CSV importMagento Import Product With image in CSVUnable to import more csv files into Magentoimport products from csvthe correct syntax for CSV Customer IMPORT? (from 1.3.1 to 1.9.1)CSV Import: editing configurable childs not workingCSV import not updating attributesImport Product Data w/ CSV from command line (not dataflow)programatically import csv - magento 2Import Product from other stores






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am trying to import magento product reviews form a csv using the following script but it seem not to be working. When I run the script I get a blank page and nothing is imported into the database although the existing ratings are removed.



please what am I doing wrong ?



// Invoke the Magento environment
require_once( 'app/Mage.php' );
Mage::app();

$_read = Mage::getSingleton('core/resource')->getConnection('core_read');
$_write = Mage::getSingleton('core/resource')->getConnection('core_write');

// Remove existing ratings
$_write->query("SET FOREIGN_KEY_CHECKS = 0;");
$_write->query("TRUNCATE TABLE rating_option_vote;");
$_write->query("TRUNCATE TABLE review_detail;");
$_write->query("TRUNCATE TABLE review_entity_summary;");
$_write->query("TRUNCATE TABLE review_store;");
$_write->query("TRUNCATE TABLE review;");
$_write->query("TRUNCATE TABLE rating_option_vote_aggregated;");
$_write->query("SET FOREIGN_KEY_CHECKS = 1;");

$file_handle = fopen('reviews-data.csv', 'r');
$i = 0;
while( $csv_line = fgetcsv($file_handle) )
if( !is_array($csv_line) )
break;

//if( $i > 50 ) break;
$i++;
// Define and clean variables
$product_id = trim($csv_line[0]);
$sku = trim($csv_line[1]);
$product_name = ucwords( strtolower( trim( $csv_line[2] ) ) );
$rating_value = (int)$csv_line[3];
if( empty( $rating_value ) )
continue;

$details = trim( $csv_line[4] );
$first_name = ucwords( strtolower( trim( $csv_line[5] ) ) );
$last_name = ucwords( strtolower( trim( $csv_line[6] ) ) );
// Create a single name
$nickname = '';
if( !empty($first_name) )
$nickname .= $first_name;

if( !empty($last_name) )
if( !empty($nickname) )
$nickname .= ' ';

$nickname .= $last_name;

if( empty($nickname) )
$nickname = 'Guest';


$email = strtolower( trim( $csv_line[7] ) );
if( !$created = strtotime( trim($csv_line[8]) ) )
$created = time();


$status = (int)$csv_line[9];
if( $status == 1 )
$approval_status = 1;
else
$approval_status = 3;


// Customer setup;
$_customer = Mage::helper('custom')->getCustomerByEmail($email); // You'll need to write this logic yourself

// IMPORTANT: Set up customer session.
// the rating/option model resource checks the customer session to get the customer ID.
$_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);

$_product = _getReviewProduct($sku); // You'll need to write this logic yourself

if(!$_product)
print '<p>Unmatched SKU: ' . $sku . '</p>';
continue;


// Add the review
$_review = Mage::getModel('review/review')
->setEntityPkValue($_product->getId())
->setStatusId($approval_status)
// ->setCreatedAt( date('Y-m-d H:m:s', $created) ) // Has no effect
->setTitle($product_name)
->setDetail($details)
->setEntityId(1)
->setStoreId(1)
->setStores(array(1,0))
->setCustomerId($_customer->getId())
->setNickname($nickname)
->save();
// Set the created date
$_write->query("UPDATE review SET created_at = '" . date('Y-m-d H:m:s', $created) . "' WHERE review_id = " . $_review->getId());
$_review->aggregate();

// Map your rating_id to your option_id with an array or something
$rating_options = array(
//1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
//2 => array(6,7,8,9,10),
//3 => array(11,12,13,14,15),
4 => array(16,17,18,19,20),
);

// Now save the ratings
foreach($rating_options as $rating_id => $option_ids):
try
$_rating = Mage::getModel('rating/rating')
->setRatingId($rating_id)
->setReviewId($_review->getId())
->addOptionVote( $option_ids[$rating_value-1],$_product->getId() );
catch (Exception $e)
print_r($e);
die($e->getMessage());

endforeach;


function _getReviewProduct( $sku )
static $cache = array();

if( !array_key_exists($sku, $cache) )
$cache[$sku] = NULL;
// You'll need to write this logic yourself



return $cache[$sku];
}









share|improve this question
































    1















    I am trying to import magento product reviews form a csv using the following script but it seem not to be working. When I run the script I get a blank page and nothing is imported into the database although the existing ratings are removed.



    please what am I doing wrong ?



    // Invoke the Magento environment
    require_once( 'app/Mage.php' );
    Mage::app();

    $_read = Mage::getSingleton('core/resource')->getConnection('core_read');
    $_write = Mage::getSingleton('core/resource')->getConnection('core_write');

    // Remove existing ratings
    $_write->query("SET FOREIGN_KEY_CHECKS = 0;");
    $_write->query("TRUNCATE TABLE rating_option_vote;");
    $_write->query("TRUNCATE TABLE review_detail;");
    $_write->query("TRUNCATE TABLE review_entity_summary;");
    $_write->query("TRUNCATE TABLE review_store;");
    $_write->query("TRUNCATE TABLE review;");
    $_write->query("TRUNCATE TABLE rating_option_vote_aggregated;");
    $_write->query("SET FOREIGN_KEY_CHECKS = 1;");

    $file_handle = fopen('reviews-data.csv', 'r');
    $i = 0;
    while( $csv_line = fgetcsv($file_handle) )
    if( !is_array($csv_line) )
    break;

    //if( $i > 50 ) break;
    $i++;
    // Define and clean variables
    $product_id = trim($csv_line[0]);
    $sku = trim($csv_line[1]);
    $product_name = ucwords( strtolower( trim( $csv_line[2] ) ) );
    $rating_value = (int)$csv_line[3];
    if( empty( $rating_value ) )
    continue;

    $details = trim( $csv_line[4] );
    $first_name = ucwords( strtolower( trim( $csv_line[5] ) ) );
    $last_name = ucwords( strtolower( trim( $csv_line[6] ) ) );
    // Create a single name
    $nickname = '';
    if( !empty($first_name) )
    $nickname .= $first_name;

    if( !empty($last_name) )
    if( !empty($nickname) )
    $nickname .= ' ';

    $nickname .= $last_name;

    if( empty($nickname) )
    $nickname = 'Guest';


    $email = strtolower( trim( $csv_line[7] ) );
    if( !$created = strtotime( trim($csv_line[8]) ) )
    $created = time();


    $status = (int)$csv_line[9];
    if( $status == 1 )
    $approval_status = 1;
    else
    $approval_status = 3;


    // Customer setup;
    $_customer = Mage::helper('custom')->getCustomerByEmail($email); // You'll need to write this logic yourself

    // IMPORTANT: Set up customer session.
    // the rating/option model resource checks the customer session to get the customer ID.
    $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);

    $_product = _getReviewProduct($sku); // You'll need to write this logic yourself

    if(!$_product)
    print '<p>Unmatched SKU: ' . $sku . '</p>';
    continue;


    // Add the review
    $_review = Mage::getModel('review/review')
    ->setEntityPkValue($_product->getId())
    ->setStatusId($approval_status)
    // ->setCreatedAt( date('Y-m-d H:m:s', $created) ) // Has no effect
    ->setTitle($product_name)
    ->setDetail($details)
    ->setEntityId(1)
    ->setStoreId(1)
    ->setStores(array(1,0))
    ->setCustomerId($_customer->getId())
    ->setNickname($nickname)
    ->save();
    // Set the created date
    $_write->query("UPDATE review SET created_at = '" . date('Y-m-d H:m:s', $created) . "' WHERE review_id = " . $_review->getId());
    $_review->aggregate();

    // Map your rating_id to your option_id with an array or something
    $rating_options = array(
    //1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
    //2 => array(6,7,8,9,10),
    //3 => array(11,12,13,14,15),
    4 => array(16,17,18,19,20),
    );

    // Now save the ratings
    foreach($rating_options as $rating_id => $option_ids):
    try
    $_rating = Mage::getModel('rating/rating')
    ->setRatingId($rating_id)
    ->setReviewId($_review->getId())
    ->addOptionVote( $option_ids[$rating_value-1],$_product->getId() );
    catch (Exception $e)
    print_r($e);
    die($e->getMessage());

    endforeach;


    function _getReviewProduct( $sku )
    static $cache = array();

    if( !array_key_exists($sku, $cache) )
    $cache[$sku] = NULL;
    // You'll need to write this logic yourself



    return $cache[$sku];
    }









    share|improve this question




























      1












      1








      1








      I am trying to import magento product reviews form a csv using the following script but it seem not to be working. When I run the script I get a blank page and nothing is imported into the database although the existing ratings are removed.



      please what am I doing wrong ?



      // Invoke the Magento environment
      require_once( 'app/Mage.php' );
      Mage::app();

      $_read = Mage::getSingleton('core/resource')->getConnection('core_read');
      $_write = Mage::getSingleton('core/resource')->getConnection('core_write');

      // Remove existing ratings
      $_write->query("SET FOREIGN_KEY_CHECKS = 0;");
      $_write->query("TRUNCATE TABLE rating_option_vote;");
      $_write->query("TRUNCATE TABLE review_detail;");
      $_write->query("TRUNCATE TABLE review_entity_summary;");
      $_write->query("TRUNCATE TABLE review_store;");
      $_write->query("TRUNCATE TABLE review;");
      $_write->query("TRUNCATE TABLE rating_option_vote_aggregated;");
      $_write->query("SET FOREIGN_KEY_CHECKS = 1;");

      $file_handle = fopen('reviews-data.csv', 'r');
      $i = 0;
      while( $csv_line = fgetcsv($file_handle) )
      if( !is_array($csv_line) )
      break;

      //if( $i > 50 ) break;
      $i++;
      // Define and clean variables
      $product_id = trim($csv_line[0]);
      $sku = trim($csv_line[1]);
      $product_name = ucwords( strtolower( trim( $csv_line[2] ) ) );
      $rating_value = (int)$csv_line[3];
      if( empty( $rating_value ) )
      continue;

      $details = trim( $csv_line[4] );
      $first_name = ucwords( strtolower( trim( $csv_line[5] ) ) );
      $last_name = ucwords( strtolower( trim( $csv_line[6] ) ) );
      // Create a single name
      $nickname = '';
      if( !empty($first_name) )
      $nickname .= $first_name;

      if( !empty($last_name) )
      if( !empty($nickname) )
      $nickname .= ' ';

      $nickname .= $last_name;

      if( empty($nickname) )
      $nickname = 'Guest';


      $email = strtolower( trim( $csv_line[7] ) );
      if( !$created = strtotime( trim($csv_line[8]) ) )
      $created = time();


      $status = (int)$csv_line[9];
      if( $status == 1 )
      $approval_status = 1;
      else
      $approval_status = 3;


      // Customer setup;
      $_customer = Mage::helper('custom')->getCustomerByEmail($email); // You'll need to write this logic yourself

      // IMPORTANT: Set up customer session.
      // the rating/option model resource checks the customer session to get the customer ID.
      $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);

      $_product = _getReviewProduct($sku); // You'll need to write this logic yourself

      if(!$_product)
      print '<p>Unmatched SKU: ' . $sku . '</p>';
      continue;


      // Add the review
      $_review = Mage::getModel('review/review')
      ->setEntityPkValue($_product->getId())
      ->setStatusId($approval_status)
      // ->setCreatedAt( date('Y-m-d H:m:s', $created) ) // Has no effect
      ->setTitle($product_name)
      ->setDetail($details)
      ->setEntityId(1)
      ->setStoreId(1)
      ->setStores(array(1,0))
      ->setCustomerId($_customer->getId())
      ->setNickname($nickname)
      ->save();
      // Set the created date
      $_write->query("UPDATE review SET created_at = '" . date('Y-m-d H:m:s', $created) . "' WHERE review_id = " . $_review->getId());
      $_review->aggregate();

      // Map your rating_id to your option_id with an array or something
      $rating_options = array(
      //1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
      //2 => array(6,7,8,9,10),
      //3 => array(11,12,13,14,15),
      4 => array(16,17,18,19,20),
      );

      // Now save the ratings
      foreach($rating_options as $rating_id => $option_ids):
      try
      $_rating = Mage::getModel('rating/rating')
      ->setRatingId($rating_id)
      ->setReviewId($_review->getId())
      ->addOptionVote( $option_ids[$rating_value-1],$_product->getId() );
      catch (Exception $e)
      print_r($e);
      die($e->getMessage());

      endforeach;


      function _getReviewProduct( $sku )
      static $cache = array();

      if( !array_key_exists($sku, $cache) )
      $cache[$sku] = NULL;
      // You'll need to write this logic yourself



      return $cache[$sku];
      }









      share|improve this question
















      I am trying to import magento product reviews form a csv using the following script but it seem not to be working. When I run the script I get a blank page and nothing is imported into the database although the existing ratings are removed.



      please what am I doing wrong ?



      // Invoke the Magento environment
      require_once( 'app/Mage.php' );
      Mage::app();

      $_read = Mage::getSingleton('core/resource')->getConnection('core_read');
      $_write = Mage::getSingleton('core/resource')->getConnection('core_write');

      // Remove existing ratings
      $_write->query("SET FOREIGN_KEY_CHECKS = 0;");
      $_write->query("TRUNCATE TABLE rating_option_vote;");
      $_write->query("TRUNCATE TABLE review_detail;");
      $_write->query("TRUNCATE TABLE review_entity_summary;");
      $_write->query("TRUNCATE TABLE review_store;");
      $_write->query("TRUNCATE TABLE review;");
      $_write->query("TRUNCATE TABLE rating_option_vote_aggregated;");
      $_write->query("SET FOREIGN_KEY_CHECKS = 1;");

      $file_handle = fopen('reviews-data.csv', 'r');
      $i = 0;
      while( $csv_line = fgetcsv($file_handle) )
      if( !is_array($csv_line) )
      break;

      //if( $i > 50 ) break;
      $i++;
      // Define and clean variables
      $product_id = trim($csv_line[0]);
      $sku = trim($csv_line[1]);
      $product_name = ucwords( strtolower( trim( $csv_line[2] ) ) );
      $rating_value = (int)$csv_line[3];
      if( empty( $rating_value ) )
      continue;

      $details = trim( $csv_line[4] );
      $first_name = ucwords( strtolower( trim( $csv_line[5] ) ) );
      $last_name = ucwords( strtolower( trim( $csv_line[6] ) ) );
      // Create a single name
      $nickname = '';
      if( !empty($first_name) )
      $nickname .= $first_name;

      if( !empty($last_name) )
      if( !empty($nickname) )
      $nickname .= ' ';

      $nickname .= $last_name;

      if( empty($nickname) )
      $nickname = 'Guest';


      $email = strtolower( trim( $csv_line[7] ) );
      if( !$created = strtotime( trim($csv_line[8]) ) )
      $created = time();


      $status = (int)$csv_line[9];
      if( $status == 1 )
      $approval_status = 1;
      else
      $approval_status = 3;


      // Customer setup;
      $_customer = Mage::helper('custom')->getCustomerByEmail($email); // You'll need to write this logic yourself

      // IMPORTANT: Set up customer session.
      // the rating/option model resource checks the customer session to get the customer ID.
      $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);

      $_product = _getReviewProduct($sku); // You'll need to write this logic yourself

      if(!$_product)
      print '<p>Unmatched SKU: ' . $sku . '</p>';
      continue;


      // Add the review
      $_review = Mage::getModel('review/review')
      ->setEntityPkValue($_product->getId())
      ->setStatusId($approval_status)
      // ->setCreatedAt( date('Y-m-d H:m:s', $created) ) // Has no effect
      ->setTitle($product_name)
      ->setDetail($details)
      ->setEntityId(1)
      ->setStoreId(1)
      ->setStores(array(1,0))
      ->setCustomerId($_customer->getId())
      ->setNickname($nickname)
      ->save();
      // Set the created date
      $_write->query("UPDATE review SET created_at = '" . date('Y-m-d H:m:s', $created) . "' WHERE review_id = " . $_review->getId());
      $_review->aggregate();

      // Map your rating_id to your option_id with an array or something
      $rating_options = array(
      //1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
      //2 => array(6,7,8,9,10),
      //3 => array(11,12,13,14,15),
      4 => array(16,17,18,19,20),
      );

      // Now save the ratings
      foreach($rating_options as $rating_id => $option_ids):
      try
      $_rating = Mage::getModel('rating/rating')
      ->setRatingId($rating_id)
      ->setReviewId($_review->getId())
      ->addOptionVote( $option_ids[$rating_value-1],$_product->getId() );
      catch (Exception $e)
      print_r($e);
      die($e->getMessage());

      endforeach;


      function _getReviewProduct( $sku )
      static $cache = array();

      if( !array_key_exists($sku, $cache) )
      $cache[$sku] = NULL;
      // You'll need to write this logic yourself



      return $cache[$sku];
      }






      magento-1.8 importexport






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 10 '14 at 15:36









      SPRBRN

      7574 gold badges15 silver badges31 bronze badges




      7574 gold badges15 silver badges31 bronze badges










      asked Jun 10 '14 at 12:35









      StifboyStifboy

      3554 silver badges16 bronze badges




      3554 silver badges16 bronze badges























          4 Answers
          4






          active

          oldest

          votes


















          3














          require_once 'app/Mage.php';
          Mage::app();


          // IMPORTANT: Set up customer session.



          // the rating/option model resource checks the customer session to get the customer ID.



          $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);


          // Add Review



          $_review = Mage::getModel('review/review');
          ->setEntityPkValue($_product->getId())
          ->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
          ->setTitle($row_source_review['Title'])
          ->setDetail($row_source_review['Review'])
          ->setEntityId(1)
          ->setStoreId($store)
          ->setStores(array($store))
          ->setCustomerId($_customer->getId())
          ->setNickname($_customer->getFirstname())
          ->save();


          Probable Error within these:



          Check last comma of the array



          $rating_options = array(
          1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
          2 => array(6,7,8,9,10),
          3 => array(11,12,13,14,15)
          );


          Probable Error within these:



          foreach($rating_options as $rating_id => $option_ids):
          try
          $_rating = Mage::getModel('rating/rating')
          ->setRatingId($rating_id)
          ->setReviewId($_review->getId())
          ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
          catch (Exception $e)
          die($e->getMessage());

          endforeach;





          share|improve this answer
































            0














            Please use following code to import the product reviews:



            <?php 
            ini_set('memory_limit', '128M');
            require_once 'path-to-Mage.php';
            Mage::app();
            $fp = fopen($fileLocation, 'r');
            Mage::app()->setCurrentStore(4); //desired store id
            while($line = fgetcsv($fp))
            $review = Mage::getModel('review/review');
            $review->setEntityPkValue($line[0]);//product id
            $review->setStatusId($line[1]);
            $review->setTitle($line[2]);
            $review->setDetail($line[3]);
            $review->setEntityId($line[4]);
            $review->setStoreId(Mage::app()->getStore()->getId());
            $review->setStatusId($line[5]); //approved
            $review->setCustomerId($line[6]);//null is for administrator
            $review->setNickname($line[7]);
            $review->setReviewId($review->getId());
            $review->setStores(array(Mage::app()->getStore()->getId()));
            $review->save();
            $review->aggregate();

            ?>





            share|improve this answer
































              0














              How to Import Magento Product Reviews & Ratings From CSV



              I am practically using the same code found in the frontend controller post action.



              html/app/code/core/Mage/Review/controllers/ProductController.php (Lines: 174-218)


              CSV Format



              1. Sku

              2. Rating

              3. Review Title

              4. Customer Email (null if guest)


              5. Review Detail



                sku1,5,My Favorite Product!,customer@email.com,"This was the best product ever!"



              PHP Script



              Things to note:



              • Both the php script and csv file should be in the root directory.

              • Rating values must be mapped to database values. The rating I use has an ID of 4. Default Magento ratings are 1, 2, 3. If you have a review with multiple ratings, create a mapping for each and loop through each rating after first creating the review.

              Database friendly rating values



              My custom rating used in this import




              require_once 'app/Mage.php'; // Load Mage

              Mage::app(); // Init Mage

              $fp = fopen('reviews.csv', 'r'); // Open CSV

              while ($line = fgetcsv($fp)) // Loop CSV lines

              $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($line[4]); // Load customer by email - Set website
              $customerId = $customer->getId(); // Save Customer ID for Later

              $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $line[0]); // Load product by SKU

              if (!$product->getId()) // Skip review if roduct is not found. Output line
              var_dump($line);
              continue;


              $ratingValues = array( // Create ratings map
              1 => 16, // 1 Star
              2 => 17, // 2 Star
              3 => 18, // 3 Star
              4 => 19, // 4 Star
              5 => 20 // 5 Star
              );

              $review = Mage::getModel('review/review'); // Get review model
              $review->setData(array( // Set Review Data
              'nickname' => $line[3],
              'title' => $line[2],
              'detail' => $line[5]
              ));

              try
              $review->setEntityId(1) // Review is for a product
              ->setEntityPkValue($product->getId()) // Relate to product
              ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED) // Status is approved
              ->setCustomerId($customer->getId()) // Set customer id... null is for guest
              ->setStoreId(1) // Set website
              ->setStores(array(1)) // Website again
              ->save(); // Pray and save
              catch (Exception $exception)
              Mage::log($exception->getMessage());


              if ($review) // If review was created
              // Loop here if you have multiple rating for review
              try
              Mage::getModel('rating/rating')
              ->setRatingId(4) // My custom rating ID
              ->setReviewId($review->getId()) // Use id of rating just created
              ->setCustomerId($customer->getId()) // Associate customer
              ->addOptionVote($ratingValues[$line[1]], $product->getId()); // Use rating mapping above to get database friendly value

              $review->aggregate(); // Not sure why... but it's needed
              catch (Exception $exception)
              Mage::log($exception->getMessage());








              share|improve this answer
































                0














                If you are familiar with Magento DB structure for reviews, I would recommend using raw SQL.

                For example:



                -- Edit values
                SET @PRODUCT_ID = 123;
                SET @STORE_ID = 1;
                SET @CUSTOMER_ID = NULL;
                SET @REVIEW_TITLE = 'Lorem Ipsum';
                SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
                SET @REVIEW_RATING = 5; -- Between 1 to 5
                SET @REVIEW_NICKNAME = 'John Doe';
                SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format

                -- No need to Edit
                SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
                SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved

                INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
                SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
                INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
                INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
                INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
                INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
                review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;


                For details on how to "Import Product Reviews in Magento via SQL", please visit
                https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/






                share|improve this answer



























                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "479"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f23611%2fmagento-import-product-reviews-from-a-csv%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  3














                  require_once 'app/Mage.php';
                  Mage::app();


                  // IMPORTANT: Set up customer session.



                  // the rating/option model resource checks the customer session to get the customer ID.



                  $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);


                  // Add Review



                  $_review = Mage::getModel('review/review');
                  ->setEntityPkValue($_product->getId())
                  ->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
                  ->setTitle($row_source_review['Title'])
                  ->setDetail($row_source_review['Review'])
                  ->setEntityId(1)
                  ->setStoreId($store)
                  ->setStores(array($store))
                  ->setCustomerId($_customer->getId())
                  ->setNickname($_customer->getFirstname())
                  ->save();


                  Probable Error within these:



                  Check last comma of the array



                  $rating_options = array(
                  1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
                  2 => array(6,7,8,9,10),
                  3 => array(11,12,13,14,15)
                  );


                  Probable Error within these:



                  foreach($rating_options as $rating_id => $option_ids):
                  try
                  $_rating = Mage::getModel('rating/rating')
                  ->setRatingId($rating_id)
                  ->setReviewId($_review->getId())
                  ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
                  catch (Exception $e)
                  die($e->getMessage());

                  endforeach;





                  share|improve this answer





























                    3














                    require_once 'app/Mage.php';
                    Mage::app();


                    // IMPORTANT: Set up customer session.



                    // the rating/option model resource checks the customer session to get the customer ID.



                    $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);


                    // Add Review



                    $_review = Mage::getModel('review/review');
                    ->setEntityPkValue($_product->getId())
                    ->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
                    ->setTitle($row_source_review['Title'])
                    ->setDetail($row_source_review['Review'])
                    ->setEntityId(1)
                    ->setStoreId($store)
                    ->setStores(array($store))
                    ->setCustomerId($_customer->getId())
                    ->setNickname($_customer->getFirstname())
                    ->save();


                    Probable Error within these:



                    Check last comma of the array



                    $rating_options = array(
                    1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
                    2 => array(6,7,8,9,10),
                    3 => array(11,12,13,14,15)
                    );


                    Probable Error within these:



                    foreach($rating_options as $rating_id => $option_ids):
                    try
                    $_rating = Mage::getModel('rating/rating')
                    ->setRatingId($rating_id)
                    ->setReviewId($_review->getId())
                    ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
                    catch (Exception $e)
                    die($e->getMessage());

                    endforeach;





                    share|improve this answer



























                      3












                      3








                      3







                      require_once 'app/Mage.php';
                      Mage::app();


                      // IMPORTANT: Set up customer session.



                      // the rating/option model resource checks the customer session to get the customer ID.



                      $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);


                      // Add Review



                      $_review = Mage::getModel('review/review');
                      ->setEntityPkValue($_product->getId())
                      ->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
                      ->setTitle($row_source_review['Title'])
                      ->setDetail($row_source_review['Review'])
                      ->setEntityId(1)
                      ->setStoreId($store)
                      ->setStores(array($store))
                      ->setCustomerId($_customer->getId())
                      ->setNickname($_customer->getFirstname())
                      ->save();


                      Probable Error within these:



                      Check last comma of the array



                      $rating_options = array(
                      1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
                      2 => array(6,7,8,9,10),
                      3 => array(11,12,13,14,15)
                      );


                      Probable Error within these:



                      foreach($rating_options as $rating_id => $option_ids):
                      try
                      $_rating = Mage::getModel('rating/rating')
                      ->setRatingId($rating_id)
                      ->setReviewId($_review->getId())
                      ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
                      catch (Exception $e)
                      die($e->getMessage());

                      endforeach;





                      share|improve this answer













                      require_once 'app/Mage.php';
                      Mage::app();


                      // IMPORTANT: Set up customer session.



                      // the rating/option model resource checks the customer session to get the customer ID.



                      $_session = Mage::getSingleton('customer/session')->setCustomer($_customer)->setCustomerAsLoggedIn($_customer);


                      // Add Review



                      $_review = Mage::getModel('review/review');
                      ->setEntityPkValue($_product->getId())
                      ->setStatusId($sc_to_mage_review_status[$row_source_review['Status']])
                      ->setTitle($row_source_review['Title'])
                      ->setDetail($row_source_review['Review'])
                      ->setEntityId(1)
                      ->setStoreId($store)
                      ->setStores(array($store))
                      ->setCustomerId($_customer->getId())
                      ->setNickname($_customer->getFirstname())
                      ->save();


                      Probable Error within these:



                      Check last comma of the array



                      $rating_options = array(
                      1 => array(1,2,3,4,5), // <== Look at your database table `rating_option` for these vals
                      2 => array(6,7,8,9,10),
                      3 => array(11,12,13,14,15)
                      );


                      Probable Error within these:



                      foreach($rating_options as $rating_id => $option_ids):
                      try
                      $_rating = Mage::getModel('rating/rating')
                      ->setRatingId($rating_id)
                      ->setReviewId($_review->getId())
                      ->addOptionVote($option_ids[$rating_value-1],$_product->getId());
                      catch (Exception $e)
                      die($e->getMessage());

                      endforeach;






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 10 '14 at 13:17









                      TBI InfotechTBI Infotech

                      4,6171 gold badge9 silver badges30 bronze badges




                      4,6171 gold badge9 silver badges30 bronze badges


























                          0














                          Please use following code to import the product reviews:



                          <?php 
                          ini_set('memory_limit', '128M');
                          require_once 'path-to-Mage.php';
                          Mage::app();
                          $fp = fopen($fileLocation, 'r');
                          Mage::app()->setCurrentStore(4); //desired store id
                          while($line = fgetcsv($fp))
                          $review = Mage::getModel('review/review');
                          $review->setEntityPkValue($line[0]);//product id
                          $review->setStatusId($line[1]);
                          $review->setTitle($line[2]);
                          $review->setDetail($line[3]);
                          $review->setEntityId($line[4]);
                          $review->setStoreId(Mage::app()->getStore()->getId());
                          $review->setStatusId($line[5]); //approved
                          $review->setCustomerId($line[6]);//null is for administrator
                          $review->setNickname($line[7]);
                          $review->setReviewId($review->getId());
                          $review->setStores(array(Mage::app()->getStore()->getId()));
                          $review->save();
                          $review->aggregate();

                          ?>





                          share|improve this answer





























                            0














                            Please use following code to import the product reviews:



                            <?php 
                            ini_set('memory_limit', '128M');
                            require_once 'path-to-Mage.php';
                            Mage::app();
                            $fp = fopen($fileLocation, 'r');
                            Mage::app()->setCurrentStore(4); //desired store id
                            while($line = fgetcsv($fp))
                            $review = Mage::getModel('review/review');
                            $review->setEntityPkValue($line[0]);//product id
                            $review->setStatusId($line[1]);
                            $review->setTitle($line[2]);
                            $review->setDetail($line[3]);
                            $review->setEntityId($line[4]);
                            $review->setStoreId(Mage::app()->getStore()->getId());
                            $review->setStatusId($line[5]); //approved
                            $review->setCustomerId($line[6]);//null is for administrator
                            $review->setNickname($line[7]);
                            $review->setReviewId($review->getId());
                            $review->setStores(array(Mage::app()->getStore()->getId()));
                            $review->save();
                            $review->aggregate();

                            ?>





                            share|improve this answer



























                              0












                              0








                              0







                              Please use following code to import the product reviews:



                              <?php 
                              ini_set('memory_limit', '128M');
                              require_once 'path-to-Mage.php';
                              Mage::app();
                              $fp = fopen($fileLocation, 'r');
                              Mage::app()->setCurrentStore(4); //desired store id
                              while($line = fgetcsv($fp))
                              $review = Mage::getModel('review/review');
                              $review->setEntityPkValue($line[0]);//product id
                              $review->setStatusId($line[1]);
                              $review->setTitle($line[2]);
                              $review->setDetail($line[3]);
                              $review->setEntityId($line[4]);
                              $review->setStoreId(Mage::app()->getStore()->getId());
                              $review->setStatusId($line[5]); //approved
                              $review->setCustomerId($line[6]);//null is for administrator
                              $review->setNickname($line[7]);
                              $review->setReviewId($review->getId());
                              $review->setStores(array(Mage::app()->getStore()->getId()));
                              $review->save();
                              $review->aggregate();

                              ?>





                              share|improve this answer













                              Please use following code to import the product reviews:



                              <?php 
                              ini_set('memory_limit', '128M');
                              require_once 'path-to-Mage.php';
                              Mage::app();
                              $fp = fopen($fileLocation, 'r');
                              Mage::app()->setCurrentStore(4); //desired store id
                              while($line = fgetcsv($fp))
                              $review = Mage::getModel('review/review');
                              $review->setEntityPkValue($line[0]);//product id
                              $review->setStatusId($line[1]);
                              $review->setTitle($line[2]);
                              $review->setDetail($line[3]);
                              $review->setEntityId($line[4]);
                              $review->setStoreId(Mage::app()->getStore()->getId());
                              $review->setStatusId($line[5]); //approved
                              $review->setCustomerId($line[6]);//null is for administrator
                              $review->setNickname($line[7]);
                              $review->setReviewId($review->getId());
                              $review->setStores(array(Mage::app()->getStore()->getId()));
                              $review->save();
                              $review->aggregate();

                              ?>






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jun 10 '14 at 12:41









                              SoftProdigySoftProdigy

                              6953 silver badges7 bronze badges




                              6953 silver badges7 bronze badges
























                                  0














                                  How to Import Magento Product Reviews & Ratings From CSV



                                  I am practically using the same code found in the frontend controller post action.



                                  html/app/code/core/Mage/Review/controllers/ProductController.php (Lines: 174-218)


                                  CSV Format



                                  1. Sku

                                  2. Rating

                                  3. Review Title

                                  4. Customer Email (null if guest)


                                  5. Review Detail



                                    sku1,5,My Favorite Product!,customer@email.com,"This was the best product ever!"



                                  PHP Script



                                  Things to note:



                                  • Both the php script and csv file should be in the root directory.

                                  • Rating values must be mapped to database values. The rating I use has an ID of 4. Default Magento ratings are 1, 2, 3. If you have a review with multiple ratings, create a mapping for each and loop through each rating after first creating the review.

                                  Database friendly rating values



                                  My custom rating used in this import




                                  require_once 'app/Mage.php'; // Load Mage

                                  Mage::app(); // Init Mage

                                  $fp = fopen('reviews.csv', 'r'); // Open CSV

                                  while ($line = fgetcsv($fp)) // Loop CSV lines

                                  $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($line[4]); // Load customer by email - Set website
                                  $customerId = $customer->getId(); // Save Customer ID for Later

                                  $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $line[0]); // Load product by SKU

                                  if (!$product->getId()) // Skip review if roduct is not found. Output line
                                  var_dump($line);
                                  continue;


                                  $ratingValues = array( // Create ratings map
                                  1 => 16, // 1 Star
                                  2 => 17, // 2 Star
                                  3 => 18, // 3 Star
                                  4 => 19, // 4 Star
                                  5 => 20 // 5 Star
                                  );

                                  $review = Mage::getModel('review/review'); // Get review model
                                  $review->setData(array( // Set Review Data
                                  'nickname' => $line[3],
                                  'title' => $line[2],
                                  'detail' => $line[5]
                                  ));

                                  try
                                  $review->setEntityId(1) // Review is for a product
                                  ->setEntityPkValue($product->getId()) // Relate to product
                                  ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED) // Status is approved
                                  ->setCustomerId($customer->getId()) // Set customer id... null is for guest
                                  ->setStoreId(1) // Set website
                                  ->setStores(array(1)) // Website again
                                  ->save(); // Pray and save
                                  catch (Exception $exception)
                                  Mage::log($exception->getMessage());


                                  if ($review) // If review was created
                                  // Loop here if you have multiple rating for review
                                  try
                                  Mage::getModel('rating/rating')
                                  ->setRatingId(4) // My custom rating ID
                                  ->setReviewId($review->getId()) // Use id of rating just created
                                  ->setCustomerId($customer->getId()) // Associate customer
                                  ->addOptionVote($ratingValues[$line[1]], $product->getId()); // Use rating mapping above to get database friendly value

                                  $review->aggregate(); // Not sure why... but it's needed
                                  catch (Exception $exception)
                                  Mage::log($exception->getMessage());








                                  share|improve this answer





























                                    0














                                    How to Import Magento Product Reviews & Ratings From CSV



                                    I am practically using the same code found in the frontend controller post action.



                                    html/app/code/core/Mage/Review/controllers/ProductController.php (Lines: 174-218)


                                    CSV Format



                                    1. Sku

                                    2. Rating

                                    3. Review Title

                                    4. Customer Email (null if guest)


                                    5. Review Detail



                                      sku1,5,My Favorite Product!,customer@email.com,"This was the best product ever!"



                                    PHP Script



                                    Things to note:



                                    • Both the php script and csv file should be in the root directory.

                                    • Rating values must be mapped to database values. The rating I use has an ID of 4. Default Magento ratings are 1, 2, 3. If you have a review with multiple ratings, create a mapping for each and loop through each rating after first creating the review.

                                    Database friendly rating values



                                    My custom rating used in this import




                                    require_once 'app/Mage.php'; // Load Mage

                                    Mage::app(); // Init Mage

                                    $fp = fopen('reviews.csv', 'r'); // Open CSV

                                    while ($line = fgetcsv($fp)) // Loop CSV lines

                                    $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($line[4]); // Load customer by email - Set website
                                    $customerId = $customer->getId(); // Save Customer ID for Later

                                    $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $line[0]); // Load product by SKU

                                    if (!$product->getId()) // Skip review if roduct is not found. Output line
                                    var_dump($line);
                                    continue;


                                    $ratingValues = array( // Create ratings map
                                    1 => 16, // 1 Star
                                    2 => 17, // 2 Star
                                    3 => 18, // 3 Star
                                    4 => 19, // 4 Star
                                    5 => 20 // 5 Star
                                    );

                                    $review = Mage::getModel('review/review'); // Get review model
                                    $review->setData(array( // Set Review Data
                                    'nickname' => $line[3],
                                    'title' => $line[2],
                                    'detail' => $line[5]
                                    ));

                                    try
                                    $review->setEntityId(1) // Review is for a product
                                    ->setEntityPkValue($product->getId()) // Relate to product
                                    ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED) // Status is approved
                                    ->setCustomerId($customer->getId()) // Set customer id... null is for guest
                                    ->setStoreId(1) // Set website
                                    ->setStores(array(1)) // Website again
                                    ->save(); // Pray and save
                                    catch (Exception $exception)
                                    Mage::log($exception->getMessage());


                                    if ($review) // If review was created
                                    // Loop here if you have multiple rating for review
                                    try
                                    Mage::getModel('rating/rating')
                                    ->setRatingId(4) // My custom rating ID
                                    ->setReviewId($review->getId()) // Use id of rating just created
                                    ->setCustomerId($customer->getId()) // Associate customer
                                    ->addOptionVote($ratingValues[$line[1]], $product->getId()); // Use rating mapping above to get database friendly value

                                    $review->aggregate(); // Not sure why... but it's needed
                                    catch (Exception $exception)
                                    Mage::log($exception->getMessage());








                                    share|improve this answer



























                                      0












                                      0








                                      0







                                      How to Import Magento Product Reviews & Ratings From CSV



                                      I am practically using the same code found in the frontend controller post action.



                                      html/app/code/core/Mage/Review/controllers/ProductController.php (Lines: 174-218)


                                      CSV Format



                                      1. Sku

                                      2. Rating

                                      3. Review Title

                                      4. Customer Email (null if guest)


                                      5. Review Detail



                                        sku1,5,My Favorite Product!,customer@email.com,"This was the best product ever!"



                                      PHP Script



                                      Things to note:



                                      • Both the php script and csv file should be in the root directory.

                                      • Rating values must be mapped to database values. The rating I use has an ID of 4. Default Magento ratings are 1, 2, 3. If you have a review with multiple ratings, create a mapping for each and loop through each rating after first creating the review.

                                      Database friendly rating values



                                      My custom rating used in this import




                                      require_once 'app/Mage.php'; // Load Mage

                                      Mage::app(); // Init Mage

                                      $fp = fopen('reviews.csv', 'r'); // Open CSV

                                      while ($line = fgetcsv($fp)) // Loop CSV lines

                                      $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($line[4]); // Load customer by email - Set website
                                      $customerId = $customer->getId(); // Save Customer ID for Later

                                      $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $line[0]); // Load product by SKU

                                      if (!$product->getId()) // Skip review if roduct is not found. Output line
                                      var_dump($line);
                                      continue;


                                      $ratingValues = array( // Create ratings map
                                      1 => 16, // 1 Star
                                      2 => 17, // 2 Star
                                      3 => 18, // 3 Star
                                      4 => 19, // 4 Star
                                      5 => 20 // 5 Star
                                      );

                                      $review = Mage::getModel('review/review'); // Get review model
                                      $review->setData(array( // Set Review Data
                                      'nickname' => $line[3],
                                      'title' => $line[2],
                                      'detail' => $line[5]
                                      ));

                                      try
                                      $review->setEntityId(1) // Review is for a product
                                      ->setEntityPkValue($product->getId()) // Relate to product
                                      ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED) // Status is approved
                                      ->setCustomerId($customer->getId()) // Set customer id... null is for guest
                                      ->setStoreId(1) // Set website
                                      ->setStores(array(1)) // Website again
                                      ->save(); // Pray and save
                                      catch (Exception $exception)
                                      Mage::log($exception->getMessage());


                                      if ($review) // If review was created
                                      // Loop here if you have multiple rating for review
                                      try
                                      Mage::getModel('rating/rating')
                                      ->setRatingId(4) // My custom rating ID
                                      ->setReviewId($review->getId()) // Use id of rating just created
                                      ->setCustomerId($customer->getId()) // Associate customer
                                      ->addOptionVote($ratingValues[$line[1]], $product->getId()); // Use rating mapping above to get database friendly value

                                      $review->aggregate(); // Not sure why... but it's needed
                                      catch (Exception $exception)
                                      Mage::log($exception->getMessage());








                                      share|improve this answer













                                      How to Import Magento Product Reviews & Ratings From CSV



                                      I am practically using the same code found in the frontend controller post action.



                                      html/app/code/core/Mage/Review/controllers/ProductController.php (Lines: 174-218)


                                      CSV Format



                                      1. Sku

                                      2. Rating

                                      3. Review Title

                                      4. Customer Email (null if guest)


                                      5. Review Detail



                                        sku1,5,My Favorite Product!,customer@email.com,"This was the best product ever!"



                                      PHP Script



                                      Things to note:



                                      • Both the php script and csv file should be in the root directory.

                                      • Rating values must be mapped to database values. The rating I use has an ID of 4. Default Magento ratings are 1, 2, 3. If you have a review with multiple ratings, create a mapping for each and loop through each rating after first creating the review.

                                      Database friendly rating values



                                      My custom rating used in this import




                                      require_once 'app/Mage.php'; // Load Mage

                                      Mage::app(); // Init Mage

                                      $fp = fopen('reviews.csv', 'r'); // Open CSV

                                      while ($line = fgetcsv($fp)) // Loop CSV lines

                                      $customer = Mage::getModel('customer/customer')->setWebsiteId(1)->loadByEmail($line[4]); // Load customer by email - Set website
                                      $customerId = $customer->getId(); // Save Customer ID for Later

                                      $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $line[0]); // Load product by SKU

                                      if (!$product->getId()) // Skip review if roduct is not found. Output line
                                      var_dump($line);
                                      continue;


                                      $ratingValues = array( // Create ratings map
                                      1 => 16, // 1 Star
                                      2 => 17, // 2 Star
                                      3 => 18, // 3 Star
                                      4 => 19, // 4 Star
                                      5 => 20 // 5 Star
                                      );

                                      $review = Mage::getModel('review/review'); // Get review model
                                      $review->setData(array( // Set Review Data
                                      'nickname' => $line[3],
                                      'title' => $line[2],
                                      'detail' => $line[5]
                                      ));

                                      try
                                      $review->setEntityId(1) // Review is for a product
                                      ->setEntityPkValue($product->getId()) // Relate to product
                                      ->setStatusId(Mage_Review_Model_Review::STATUS_APPROVED) // Status is approved
                                      ->setCustomerId($customer->getId()) // Set customer id... null is for guest
                                      ->setStoreId(1) // Set website
                                      ->setStores(array(1)) // Website again
                                      ->save(); // Pray and save
                                      catch (Exception $exception)
                                      Mage::log($exception->getMessage());


                                      if ($review) // If review was created
                                      // Loop here if you have multiple rating for review
                                      try
                                      Mage::getModel('rating/rating')
                                      ->setRatingId(4) // My custom rating ID
                                      ->setReviewId($review->getId()) // Use id of rating just created
                                      ->setCustomerId($customer->getId()) // Associate customer
                                      ->addOptionVote($ratingValues[$line[1]], $product->getId()); // Use rating mapping above to get database friendly value

                                      $review->aggregate(); // Not sure why... but it's needed
                                      catch (Exception $exception)
                                      Mage::log($exception->getMessage());









                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Apr 29 '18 at 16:52









                                      Thomas JacksonThomas Jackson

                                      1




                                      1
























                                          0














                                          If you are familiar with Magento DB structure for reviews, I would recommend using raw SQL.

                                          For example:



                                          -- Edit values
                                          SET @PRODUCT_ID = 123;
                                          SET @STORE_ID = 1;
                                          SET @CUSTOMER_ID = NULL;
                                          SET @REVIEW_TITLE = 'Lorem Ipsum';
                                          SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
                                          SET @REVIEW_RATING = 5; -- Between 1 to 5
                                          SET @REVIEW_NICKNAME = 'John Doe';
                                          SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format

                                          -- No need to Edit
                                          SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
                                          SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved

                                          INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
                                          SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
                                          INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
                                          INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
                                          INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
                                          INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
                                          review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;


                                          For details on how to "Import Product Reviews in Magento via SQL", please visit
                                          https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/






                                          share|improve this answer





























                                            0














                                            If you are familiar with Magento DB structure for reviews, I would recommend using raw SQL.

                                            For example:



                                            -- Edit values
                                            SET @PRODUCT_ID = 123;
                                            SET @STORE_ID = 1;
                                            SET @CUSTOMER_ID = NULL;
                                            SET @REVIEW_TITLE = 'Lorem Ipsum';
                                            SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
                                            SET @REVIEW_RATING = 5; -- Between 1 to 5
                                            SET @REVIEW_NICKNAME = 'John Doe';
                                            SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format

                                            -- No need to Edit
                                            SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
                                            SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved

                                            INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
                                            SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
                                            INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
                                            INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
                                            INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
                                            INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
                                            review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;


                                            For details on how to "Import Product Reviews in Magento via SQL", please visit
                                            https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/






                                            share|improve this answer



























                                              0












                                              0








                                              0







                                              If you are familiar with Magento DB structure for reviews, I would recommend using raw SQL.

                                              For example:



                                              -- Edit values
                                              SET @PRODUCT_ID = 123;
                                              SET @STORE_ID = 1;
                                              SET @CUSTOMER_ID = NULL;
                                              SET @REVIEW_TITLE = 'Lorem Ipsum';
                                              SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
                                              SET @REVIEW_RATING = 5; -- Between 1 to 5
                                              SET @REVIEW_NICKNAME = 'John Doe';
                                              SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format

                                              -- No need to Edit
                                              SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
                                              SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved

                                              INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
                                              SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
                                              INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
                                              INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
                                              INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
                                              INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
                                              review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;


                                              For details on how to "Import Product Reviews in Magento via SQL", please visit
                                              https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/






                                              share|improve this answer













                                              If you are familiar with Magento DB structure for reviews, I would recommend using raw SQL.

                                              For example:



                                              -- Edit values
                                              SET @PRODUCT_ID = 123;
                                              SET @STORE_ID = 1;
                                              SET @CUSTOMER_ID = NULL;
                                              SET @REVIEW_TITLE = 'Lorem Ipsum';
                                              SET @REVIEW_DETAIL = 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...';
                                              SET @REVIEW_RATING = 5; -- Between 1 to 5
                                              SET @REVIEW_NICKNAME = 'John Doe';
                                              SET @REVIEW_CREATED_AT = '2019-07-15'; -- OR date in YY-mm-dd HH:ii:ss format

                                              -- No need to Edit
                                              SET @REVIEW_ENTITY_ID = (SELECT entity_id FROM rating_entity WHERE entity_code = 'product'); -- 1: product, ...
                                              SET @REVIEW_STATUS_ID = (SELECT status_id FROM review_status WHERE status_code = 'Pending'); -- 1: Approved, 2: Pending, 3: Not Approved

                                              INSERT INTO review SET created_at = @REVIEW_CREATED_AT, entity_id = @REVIEW_ENTITY_ID, entity_pk_value = @PRODUCT_ID, status_id = @REVIEW_STATUS_ID;
                                              SET @REVIEW_ID = (SELECT LAST_INSERT_ID());
                                              INSERT INTO review_detail SET review_id = @REVIEW_ID, store_id = @STORE_ID, title = @REVIEW_TITLE, detail = @REVIEW_DETAIL, nickname = @REVIEW_NICKNAME, customer_id = @CUSTOMER_ID;
                                              INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = 0;
                                              INSERT INTO review_store SET review_id = @REVIEW_ID, store_id = @STORE_ID;
                                              INSERT INTO rating_option_vote SET option_id = 5, remote_ip = '', remote_ip_long = 0, customer_id = @CUSTOMER_ID, entity_pk_value = @PRODUCT_ID, rating_id = @REVIEW_ENTITY_ID,
                                              review_id = @REVIEW_ID, percent = 100, value = @REVIEW_RATING;


                                              For details on how to "Import Product Reviews in Magento via SQL", please visit
                                              https://blog.magepsycho.com/import-product-reviews-in-magento-via-sql/







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Aug 3 at 9:08









                                              MagePsychoMagePsycho

                                              3,4231 gold badge19 silver badges46 bronze badges




                                              3,4231 gold badge19 silver badges46 bronze badges






























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Magento Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f23611%2fmagento-import-product-reviews-from-a-csv%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                                                  Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                                                  Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form