0
votes

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?

1
What you get if you do a var_dump of colletion before the foreach?Aurelio De Rosa

1 Answers

2
votes

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();