Search code examples
magentocategoriesmagento-soap-api

How to create subcategories in Magento using API SOAP?


I am using Magento standard API SOAP to create categories and sub-categories.

Here is the code i am using to insert categories:

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

$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'catalog_category.create', array(2, array(
'name' => 'Category name',
'is_active' => 1,
'position' => 1,

'available_sort_by' => 'position',
'custom_design' => null,
'custom_apply_to_products' => null,
'custom_design_from' => null,
'custom_design_to' => null,
'custom_layout_update' => null,
'default_sort_by' => 'position',
'description' => 'Category description',
'display_mode' => null,
'is_anchor' => 0,
'landing_page' => null,
'meta_description' => 'Category meta description',
'meta_keywords' => 'Category meta keywords',
'meta_title' => 'Category meta title',
'page_layout' => 'two_columns_left',
'url_key' => 'url-key',
'include_in_menu' => 1,
)));

Til here everything is ok, and success, But i am really confuse how to put subcategories under the categories i have created. I have tried using catalog_category.move but no result.

Any of you been through this trick? Thank you


Solution

  • $category = Mage::getModel('catalog/category');
    $category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().
    
    //if update
    if ($id) {
      $category->load($id);
    }
    
    $general['name'] = "My Category";
    $general['path'] = "1/3/"; // catalog path here you can add your own ID
    $general['description'] = "Great My Category";
    $general['meta_title'] = "My Category"; //Page title
    $general['meta_keywords'] = "My , Category";
    $general['meta_description'] = "Some description to be found by meta search robots. 2";
    $general['landing_page'] = ""; //has to be created in advance, here comes id
    $general['display_mode'] = "PRODUCTS"; //static block and the products are shown on the page
    $general['is_active'] = 1;
    $general['is_anchor'] = 0;
    $general['page_layout'] = 'two_columns_left';
    
    //$general['url_key'] = "cars";//url to be used for this category's page by magento.
    //$general['image'] = "cars.jpg";
    
    
    $category->addData($general);
    
    try {
        $category->setId(255); // Here you cant set your own entity id
        $category->save();
        echo "Success! Id: ".$category->getId();
    }
    catch (Exception $e){
        echo $e->getMessage();
    }