Take the 2-minute tour ×


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

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?

share|improve this question
What you get if you do a var_dump of colletion before the foreach? –  Aurelio De Rosa Oct 22 '11 at 13:01
add comment

1 Answer

up vote 1 down vote accepted

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


To change the categories you could try something like this:

$categories = $product->getCategoryIds();
$categories[] = 4; // Category to add
share|improve this answer
add comment

Your Answer


