Skip to content

Instantly share code, notes, and snippets.

@seansan
Created December 3, 2018 10:43
Show Gist options
  • Save seansan/07919bfa86e23bb2cd2f9922f89b1cbd to your computer and use it in GitHub Desktop.
Save seansan/07919bfa86e23bb2cd2f9922f89b1cbd to your computer and use it in GitHub Desktop.
Magento 1.x script to copy product reviews from storeview A to B (and v.v.)
<?php
error_reporting(~0); ini_set('display_errors', 1);
// adjust for where your file is, ours is in protected subdirectory of /
define('MAGENTO_ROOT', dirname(dirname(__FILE__)));
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
$test = true; // if set to false then data will be written
$resource = Mage::getSingleton('core/resource');
$adapter = $resource->getConnection('core_write');
$prefix = Mage::getConfig()->getTablePrefix();
$tableName = $prefix.'review_store';
$values = array();
$query = 'SELECT * FROM ' . $tableName;
$ids = $adapter->fetchAll($query);
// copy_reviews_tostore(idlist,valuelist,from_storeid_here,to_storeid_array_here)
copy_reviews_tostore($ids,$values,1,array(8));
//copy_reviews_tostore($ids,$values,7,array(2,8));
//copy_reviews_tostore($ids,$values,2,array(8));
try {
foreach ($values as $value) { $result .= "(".$value['review_id'].",".$value['store_id'].")".",\n"; }
$result=substr($result, 0, -2);
$query="INSERT INTO $tableName (`review_id`,`store_id`) VALUES $result;";
if ($test) {
// if you want to copy-paste manual to PhpMyadmin
echo($query);
} else {
// if you want to execute directly
$adapter->beginTransaction();
$adapter->query($query);
$adapter->commit();
}
echo "OK\n";
} catch (Exception $e) {
$adapter->rollBack();
print_r($e->getTrace());
}
function copy_reviews_tostore ($ids, &$values, $from_store, $to_stores) {
foreach ($ids as $id) {
$review_id = $id['review_id'];
$store_id = $id['store_id'];
if($store_id != $from_store) { continue; }
foreach ($to_stores as $store) {
// do not write to same store
if($store_id == $store) { continue; }
// check for already existing review/store value in DB
if (check_review_exists($ids, $store, $review_id)) { continue; }
// check for already existing review/store in write buffer (from previous cycle)
$skip = false;
foreach ($values as $value) {
if (empty($values) || ($id['review_id'] == $value['review_id'] && $store == $value['store_id']))
{ $skip = true; }
}
if (!$skip) { $values[]= array("review_id" => $review_id,"store_id" => $store); }
}
}
}
function check_review_exists ($ids, $find_store_id, $find_review_id) {
$ret = false;
foreach ($ids as $id) {
$review_id = $id['review_id'];
$store_id = $id['store_id'];
if ($review_id == $find_review_id && $store_id == $find_store_id) { $ret = true; }
}
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment