Fragmented Thought

Magento import reviews

By

Published:

Lance Gliser

Heads up! This content is more than six months old. Take some time to verify everything still works as expected.

Need to import reviews from your old system to Magento? Try this code as a starting point.

// 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]; }