Magento – How to insert Categories into Magento programmatically

apicategoryimportmagento-1.6

This is the scenario:

  1. I am making a SOAP call from Magento to a CMS
  2. The CMS will produce and return a json encoded object of new Category names and some category properties ( Title, URL, Parent/Child relationship, that's pretty much it ) to Magento.

  3. I need to iterate through this object, and take the Category names and insert them into the Magento DB so that Magento can manage these categories. I will also need to add whatever category properties to each category that aren't present in the object, and I need to correctly insert these Categories so that they show up in the admin section under Manage Categories.

My Question: I just need to know how to work with Magento's API to insert the Categories into the database correctly. Can anyone point me in the right direction?

Note: I'm using Magento CE 1.6.1.0. Using a plugin won't work in my case, because I need this to happen seamlessly to the user logging into the admin area. If there's a plugin someone knows of that is open source and has the functionality in it that I'm looking for, please share the name so I can study that code.

Thanks for any help!

Best Answer

Magento also has an API, accessible via SOAP and XML-RPC, and it includes the ability to create a category.

If you're not interested in learning how to make a SOAP call into Magento, you can take a look at the implementation of the category create method at

#File: app/code/core/Mage/Catalog/Model/Category/Api.php
public function create($parentId, $categoryData, $store = null)
{
    //...PHP code to create a category is here
}

and you could call the category create method manually with something like this

Mage::getModel('catalog/category_api')->create($parentId, $categoryData);

I've never seen a full explanation of the raw database structure behind categories, which is extra confusing given it's both EAV, and representing a tree structure in a SQL table. I wouldn't attempt a category insert via raw SQL calls.

Related Topic