Skip to content

Instantly share code, notes, and snippets.

@lewisvoncken
Last active November 24, 2020 21:29
Show Gist options
  • Save lewisvoncken/65674868675da1e2eb6c821d2d75a270 to your computer and use it in GitHub Desktop.
Save lewisvoncken/65674868675da1e2eb6c821d2d75a270 to your computer and use it in GitHub Desktop.
Duplicate Script Category Magento 2 enterprise (will probably also work for Magento 1)
<?php
// use shell_exec("mysql -e '{$sql}'"); for mysql_query or use pdo or something
$catId = 2;
$host = "";
$username = "";
$password = "";
$dbname = "";
$res = mysql_pconnect($host, $username, $password);
mysql_select_db($dbname);
$catsDone = 0;
duplicate_entity($catId);
// Fix the children count for all (sub)categories
$sql = "CREATE TABLE catalog_category_entity_tmp LIKE catalog_category_entity";
mysql_query($sql);
$sql = "INSERT INTO catalog_category_entity_tmp SELECT * FROM catalog_category_entity";
mysql_query($sql);
$sql = "UPDATE catalog_category_entity cce SET children_count = ( SELECT count(cce2.row_id) – 1 as children_county FROM catalog_category_entity_tmp cce2 WHERE PATH LIKE CONCAT(cce.path,'%') )";
mysql_query($sql);
$sql = "DROP TABLE catalog_category_entity_tmp";
mysql_query($sql);
echo $catsDone . '<p>Categories duplicated.</p>';
function duplicate_entity($id, $parent_id = null) {
global $catsDone;
mysql_query("SET NAMES 'utf8'");
// Grab category to copy
$sql = "SELECT * FROM catalog_category_entity WHERE row_id = " . $id;
$query_entity = mysql_query($sql);
$entity = mysql_fetch_object($query_entity);
if(!$parent_id) $parent_id = $entity->parent_id;
$query = mysql_query("SELECT sequence_value FROM sequence_catalog_category ORDER BY sequence_value DESC LIMIT 1");
$parent = mysql_fetch_object($query);
$sequenceValue = $parent->sequence_value + 1;
mysql_query("INSERT INTO sequence_catalog_category (sequence_value) VALUES({$sequenceValue})");
mysql_query("INSERT INTO catalog_category_entity (entity_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count) VALUES ({$sequenceValue}, {$entity->attribute_set_id}, {$parent_id}, NOW(), NOW(), '', {$entity->position}, {$entity->level}, {$entity->children_count})");
$newEntityId = mysql_insert_id();
$query = mysql_query("SELECT path FROM catalog_category_entity WHERE row_id = " . $parent_id);
$parent = mysql_fetch_object($query);
$path = $parent->path . '/' . $newEntityId;
mysql_query("UPDATE catalog_category_entity SET path='". $path."' WHERE row_id=". $newEntityId);
foreach(array('datetime', 'decimal', 'int', 'text', 'varchar') as $dataType) {
$sql = "SELECT * FROM catalog_category_entity_" . $dataType . " WHERE row_id=" . $entity->row_id;
$query = mysql_query($sql);
while ($value = mysql_fetch_object($query)) if (is_null($value->value)) {
mysql_query("INSERT INTO catalog_category_entity_".$dataType." (attribute_id, store_id, row_id, value) VALUES ({$value->attribute_id}, {$value->store_id}, {$newEntityId}, NULL)");
} else {
mysql_query("INSERT INTO catalog_category_entity_".$dataType." (attribute_id, store_id, row_id, value) VALUES ({$value->attribute_id}, {$value->store_id}, {$newEntityId}, '{$value->value}')");
}
}
//for Products
$sql = "SELECT * FROM catalog_category_product WHERE category_id = " . $id;
$query = mysql_query($sql);
while ($value = mysql_fetch_object($query)) {
$sql = "INSERT INTO catalog_category_product (category_id, product_id, position) VALUES ({$sequenceValue},{$value->product_id},{$value->position})";
echo $sql;
if(!mysql_query($sql))
echo("Error");
}
$sql = "SELECT row_id FROM catalog_category_entity WHERE parent_id = " . $id;
$query = mysql_query($sql);
while ($entity = mysql_fetch_object($query)) {
duplicate_entity($entity->row_id, $newEntityId);
}
$catsDone++;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment