Sometime we need to copy same products to another category.(Like we have two stores with same category )

Adding product from back-end is very time consuming process you can do it by code.

You can create a file in your root directory copy-products.php with the following code to copy product:

<?php
require_once ( "app/Mage.php" );
umask(0);

// Initialize Magento
Mage::app();

$category = Mage::getModel('catalog/category');
$category->load('24'); // Category id you want to copy
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $product) {
       $product->getId();// Now get category ids and add a specific category to them and save?
       $categories = $product->getCategoryIds();
       $categories[] = 29; // Category id you want to add
       $product->setCategoryIds($categories);
       $product->save();
}
?>

Categories: Magento

2 Comments

Joe · July 21, 2016 at 7:08 pm

This script is useful and has saved me many hours manually going through products and assigning categories, however It seems to miss a few products out that I then have to assign manually.

E.g. Category has 46 products in it, I run the script and it only copies over 38.

Is this something we can reduce so it copies all of the products over?

rahul · January 11, 2017 at 12:53 pm

where i pass this code

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *