Alexander Wolff
Alexander Wolff

Reputation: 152

Magento 1.6 copy category products

Heyhey,

I'm trying to make a script that will load all products from one category and add them to another category (so, basically, just link all products to an additional category). What I'm trying is:

$category = Mage::getModel('catalog/category');
$category->load($id); // Preset category id
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');

foreach ($collection as $product) {
    $result[] = $product->getProductId();
// Now get category ids and add a specific category to them and save?
}

$result comes up empty, and I have no idea how to proceed. Any ideas?

Upvotes: 0

Views: 928

Answers (1)

Zifius
Zifius

Reputation: 1650

First thing to look at, don't select all attributes, $collection->addAttributeToSelect('id') is enough. Second to get product ID use

$product->getId();

To change the categories you could try something like this:

$categories = $product->getCategoryIds();
$categories[] = 4; // Category to add
$product->setCategoryIds($categories);
$product->save();

Upvotes: 2

Related Questions