Last active
November 12, 2021 22:08
-
-
Save janzikmund/a92e0a219af5e362fa1293b52b6b831e to your computer and use it in GitHub Desktop.
Move / merge all product attributes from Magento storeview level to Default Scope and erases them on storeview level so Default takes priority everywhere
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Script to migrate all data from storeview 1 to Default Scope (storeview 0) | |
@author - Jan Zikmund | |
*/ | |
$store_from = 1; | |
$store_to = 0; | |
$tables = array( | |
'catalog_product_entity_varchar', | |
'catalog_product_entity_text', | |
'catalog_product_entity_datetime', | |
'catalog_product_entity_decimal', | |
'catalog_product_entity_int', | |
); | |
$entity_type = 4; // product | |
// set environment and config to database | |
switch ($_SERVER['HTTP_HOST']) { | |
case 'myside.dev': | |
// my local | |
$db = array( | |
'dsn' => 'mysql:dbname=your_db_name;host=localhost', | |
'username' => 'example_user', | |
'password' => 'example_pass', | |
); | |
break; | |
case 'www.mysite.com.au': | |
// production | |
$db = array( | |
'dsn' => 'mysql:dbname=your_db_name;host=localhost', | |
'username' => 'example_user', | |
'password' => 'example_pass', | |
); | |
break; | |
default: | |
// no environment set, terminate | |
die('Error - Environment not recognized. Terminating.'); | |
} | |
// set up DB connection | |
$db = new PDO($db['dsn'], $db['username'], $db['password'], array ( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION | |
)); | |
// go through tables | |
foreach($tables as $table) { | |
echo " ----- PROCESSING TABLE $table ------ <br /><br /><br />"; | |
// query all values from current storeview that are not null | |
$sql_from = " | |
SELECT * FROM $table | |
WHERE store_id = $store_from | |
AND entity_type_id = $entity_type | |
AND value IS NOT NULL | |
"; | |
// query attributes in new storeview | |
$sql_to = " | |
SELECT * FROM $table | |
WHERE store_id = $store_to | |
AND entity_type_id = $entity_type | |
"; | |
// load all and new attributes | |
$attributes_from = $db->query($sql_from)->FetchAll(); | |
$attributes_to = $db->query($sql_to)->FetchAll(); | |
// transform structure of to_ attributes array for easier search | |
$att_to_sorted = array(); | |
foreach($attributes_to as $a) { | |
$att_to_sorted[$a['attribute_id'] . '-' . $a['entity_id']] = $a; | |
} | |
foreach($attributes_from as $att_from) { | |
// go through attributes from, check if they are on new storeview and update them | |
$key = $att_from['attribute_id'] . '-' . $att_from['entity_id']; | |
if (isset($att_to_sorted[$key])) { | |
// we have matching value to, check if we can transform it | |
$value_from = $att_from['value']; | |
$value_to = $att_to_sorted[$key]['value']; | |
if ($value_from !== '' && $value_from !== $value_to) { | |
// difference - copy FROM value to TO table | |
$field_id_to = $att_to_sorted[$key]['value_id']; | |
// log | |
echo "diff: <br />"; | |
echo "<strong>$value_to</strong> <--- was replaced by <br /><strong>$value_from</strong><br />"; | |
echo $table . ": #$field_id_to <--- replaced by #" . $att_from['value_id'] . "<br /><br />"; | |
// update it in database | |
// $sql_update = $db->prepare("UPDATE $table SET value = ? WHERE value_id = ?"); | |
// $sql_update->execute( array( 0 => $value_from, 1 => $field_id_to) ); | |
} | |
} | |
} | |
// after all attributes have been transformed, remove all attributes from old storeview | |
// $sql_truncate = $db->prepare("DELETE FROM $table WHERE store_id = ? AND entity_type_id = ?"); | |
// $res = $sql_truncate->execute( array( $store_from, $entity_type ) ); | |
echo " ----- truncated table $table ------ <br /><br />"; | |
echo " -----------------------------------<br /><br />"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment