Showing posts with label copy products. Show all posts
Showing posts with label copy products. Show all posts

Monday, 25 May 2015

Copy products from one category to another in Magento

Hi sometime we need to copy the products from one caegory to another in magento , but by default there is no such option like this in magento. So we can do this programatically with the below code




<?php
$category = Mage::getModel('catalog/category');
$category->load('2'); // Preset category id
$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[] = 3; // Category to add
$product->setCategoryIds($categories);
$product->save();

}