Programmatically add Magento products to categories

Programmatically add Magento products to categories

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Programmatically add Magento products to categories

Managing hundreds of categories for over 21,000 products is no small feat in Magento—especially when many items need to be placed in multiple categories. While the admin interface allows manual assignment, this approach quickly becomes impractical as your product catalog grows.

Fortunately, you can use Magento’s API and custom scripts to programmatically add products to categories, saving significant time and reducing errors compared to manual data entry. Let’s explore how to accomplish this efficiently.

Using the Magento SOAP API

Magento provides a SOAP API that allows external applications to interact with its core functionality, including category management. By utilizing this API, you can create a script that automatically assigns products to their respective categories based on pre-defined rules or imported data.

To get started, you’ll need to enable the API in your Magento admin panel:

  1. Log in to your Magento backend.
  2. Navigate to System > Web Services > SOAP XML-RPC.
  3. Set “Enable Web Services” to Yes.
  4. Save the configuration changes.

Next, create a PHP script that connects to your Magento site and uses the appropriate SOAP client classes to add products to categories. You can find detailed documentation on using the Magento SOAP API in the official Magento Developer Documentation.

Creating a custom script

Let’s assume you have an array of product IDs and their corresponding category ID(s) to which they should be assigned. Here’s a simplified example of how your script could interact with the Magento SOAP API:

<?php

$client = new SoapClient('http://your_magento_site.com/api/soap/?wsdl');

$sessionId = $client->login('apiUser', 'apiKey');
$category = array(
    'category_id' => 4,
    'name'        => 'New Category',
    'is_active'   => 1
);

$productIds = array(5, 7, 8); // example product IDs

foreach ($productIds as $productId) {
    try {
        $result = $client->catalogCategoryAssignProduct(
            $sessionId,
            $category['category_id'],
            $productId,
            true
        );
        echo "Assigned product $productId to category {$category['name']}\n";
    } catch (SoapFault $fault) {
        echo "Error assigning product $productId: " . $fault->getMessage() . "\n";
    }
}

$client->endSession($sessionId);

In this example, replace 'http://your_magento_site.com/api/soap/?wsdl' with the WSDL URL of your Magento site. Also, ensure that you have configured an API user and key with sufficient privileges to manage categories.

Optimizing performance for large datasets

When dealing with thousands or tens of thousands of products, it’s crucial to optimize your script for performance:

  1. Bulk processing: Instead of processing one product at a time, try to batch multiple assignments together using the catalogCategoryAssignProduct API method in loops.
  2. Database indexing: Ensure that proper indexes are applied to the Magento database tables involved in category assignment (e.g., catalog_product_entity, catalog_category_entity). This can significantly speed up queries and reduce script execution times.

Conclusion

Programmatically adding products to categories is a powerful way to streamline your product management workflow as your Magento site grows. By utilizing the SOAP API and custom scripts, you can automate this process and save valuable time that would otherwise be spent on manual data entry.

Remember, proper error handling and performance optimization are essential when working with large datasets in Magento. Always test your scripts thoroughly before deploying them to a production environment.

At Laravel Company, our team of experienced Magento developers can help you optimize your product catalog management and implement custom solutions tailored to your specific business needs. Reach out to us today to learn more about how we can assist you with your Magento development projects!

Catalog API Admin Configuration

Our network