1

I want to copy all products from one category to another category in Magento 1.9.2.2?

2 Answers 2

1

You can try below script to copy products from one category to other.

<?php
require_once('app/Mage.php');
umask(0);
Mage::app('admin');

error_reporting(1);
set_time_limit(0);
ini_set('memory_limit', '2048M');

//  COPY Products From => To
//  5 => 10
$from = 5;
$to = 10;

$category = Mage::getModel('catalog/category')->load($from);

$productCollection = $category->setStoreId(1)->getProductCollection();

foreach($productCollection as $_product) {
  $product = Mage::getModel('catalog/product')->load($_product->getId());

  $newCategories = $origCats = $product->getCategoryIds();
  if(!in_array($to, $origCats)) {
    $newCategories = array_merge($origCats, array($to));
    $product->setCategoryIds($newCategories)->save();
    echo 'Assigned -- ' . $product->getId() . '<br />';
  }
}
echo 'Completed Execution!!!';
?>

Hope it helps!!!

0
0

Upload below script in magento 1 root and then run below URL

http://www.example.com/copycat.php?id=[CATEGORY ID TO COPY]

<?php
if(!is_numeric($_GET['id']))die('Please specify a category ID');

$catId = $_GET['id'];

$xml = simplexml_load_file('app/etc/local.xml');
$host = $xml->global->resources->default_setup->connection->host;
$username = $xml->global->resources->default_setup->connection->username;
$password = $xml->global->resources->default_setup->connection->password;
$dbname = $xml->global->resources->default_setup->connection->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.entity_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 . ' Categories duplicated.';

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 entity_id = " . $id;
$query_entity = mysql_query($sql);

$entity = mysql_fetch_object($query_entity);

if(!$parent_id)$parent_id = $entity->parent_id;

mysql_query("INSERT INTO catalog_category_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, path, position, level, children_count)
VALUES ({$entity->entity_type_id}, {$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 entity_id = " . $parent_id);
$parent = mysql_fetch_object($query);
$path = $parent->path . '/' . $newEntityId;

mysql_query("UPDATE catalog_category_entity SET path='". $path."' WHERE entity_id=". $newEntityId);

foreach(array('datetime', 'decimal', 'int', 'text', 'varchar') as $dataType){
$sql = "SELECT * FROM catalog_category_entity_".$dataType."
WHERE entity_id=" . $entity->entity_id;
//die($sql);
$query = mysql_query($sql);
while ($value = mysql_fetch_object($query)) if (is_null($value->value)) {
mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value)
VALUES ({$value->entity_type_id}, {$value->attribute_id}, {$value->store_id}, {$newEntityId}, NULL)");
} else {
mysql_query("INSERT INTO catalog_category_entity_".$dataType." (entity_type_id, attribute_id, store_id, entity_id, value)
VALUES ({$value->entity_type_id}, {$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 ({$newEntityId},{$value->product_id},{$value->position})";
echo $sql;
if(!mysql_query($sql))
echo("Error");
}

$sql = "SELECT entity_id FROM catalog_category_entity WHERE parent_id = " . $id;
$query = mysql_query($sql);

while ($entity = mysql_fetch_object($query)){
duplicate_entity($entity->entity_id, $newEntityId);
}
$catsDone++;

}

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.