0

HI i have a magento root category in which there is about 1 million products

. Now i need to copy this category products to another category

can you please suggest me how can i do this

Because in admin i have try to create a new category and assign products

but in one time i can assign 1500 and that will take so long if i do that manually. so please suggest me how can i do this.

Community
  • 1
  • 1
Rohit Goel
  • 3,396
  • 8
  • 56
  • 107

1 Answers1

2

let's say that the category with the products has the id 10.
And you want to copy all the products to category with id 20.
Run this script.

$sourceId = 10;
$destinationId = 20;
$source = Mage::getModel('catalog/category')->load($sourceId);
$destination = Mage::getModel('catalog/category')->load($destinationId);

$products = $source->getProductsPosition();
$destination->setPostedProducts($products);
$destination->save();

But keep in mind that any product you have in the destination category will be removed from that category.

If you want to keep the products you already have in the destination category use the following.

$sourceId = 10;
$destinationId = 20;
$source = Mage::getModel('catalog/category')->load($sourceId);
$destination = Mage::getModel('catalog/category')->load($destinationId);

$products = $source->getProductsPosition();
$destinationProducts = $destination->getProductsPosition();
$destination->setPostedProducts(array_merge($products, $destinationProducts));
$destination->save();
Marius
  • 15,148
  • 9
  • 56
  • 76
  • I'm trying to use this +Marius but I keep getting a foreign key error... Does this work for you? – WebDevB Apr 18 '16 at 23:08