How to assign categories for products in magento Programmatically

How to assign categories for products in magento Programmatically

July 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to assign categories for products in magento Programmatically

Optimizing Product Category Assignments in Magento

As an e-commerce developer, you often find yourself looking for ways to streamline common tasks like assigning products to categories. In this post, we’ll explore how to optimize the process of mass assigning multiple products to multiple categories in Magento.

The Current Approach: Slow and Inefficient

The current approach many developers take is to iterate over each product in a collection, retrieve its existing category IDs, merge them with the new category IDs, set those merged IDs back on the product, and then save it. Here’s an example:

$collection = Mage::getModel('catalog/product')->getCollection();
$category_ids = array(1, 2, 3, 4, 5);

if ($category_ids != NULL && $collection->getData() != NULL)
{
    foreach ($collection as $product)
    {
        $categories_pd = $product->getCategoryIds();
        $product->setCategoryIds(array_merge($product->getCategoryIds(), array($category_ids)));
        $product->save();
    }
}

While this code works, it’s slow and inefficient for a few reasons:

  1. It retrieves the existing category IDs for every product in the loop.
  2. It merges arrays for each iteration.
  3. It saves the product after every single iteration.

For 200 products, this can take up to two minutes as you’ve experienced.

The Efficient Approach: Bulk Assigning

To optimize this process, we need to use Magento’s catalog_category_product table directly and insert new rows in bulk instead of updating existing ones one by one. This approach eliminates the need for retrieving and merging category IDs for each product.

Here’s how you can do it:

  1. First, fetch all your products’ IDs into an array using a single query:
$product_ids = Mage::getResourceModel('catalog/product_collection')->getAllIds();
  1. Prepare the data to be inserted in bulk format. For each product ID, create an array containing the product ID and the new category IDs you want to assign.

  2. Use Magento’s resource model to prepare a insert array:

$insert_array = array();
foreach ($category_ids as $category_id) {
    foreach ($product_ids as $product_id) {
        $insert_array[] = array(
            'entity_id' => $product_id,
            'category_id' => $category_id
        );
    }
}
  1. Finally, insert the data in bulk using the catalog_category_product table:
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');

$tableName = $resource->getTableName('catalog/category_product');
$writeConnection->insertMultiple($tableName, $insert_array);

This approach avoids the need to retrieve and merge arrays for each product. It also skips the slow save() method by directly inserting rows into the database, which is significantly faster.

Conclusion

Optimizing the process of assigning categories in Magento requires a shift from updating products one at a time to directly manipulating the catalog_category_product table. By using bulk insertion instead of iterative updates, you can dramatically reduce the time it takes to assign multiple products to multiple categories.

Remember that this approach is faster but may have performance implications for very large datasets or if used too frequently. Always test changes like these in a development environment first.

For more advanced techniques and Magento optimization strategies, check out our detailed guides at Laravel Company.

Catalog Database Performance

Our network