Category API

Last modified by Killoff on Sat, July 9, 2011 02:48
Source|Old Revisions  

This is an old revision of the document!


Allows to export/import categories from/to Magento.

Module: Mage_Catalog

Resource: catalog_category

Aliases:

  • category

Methods

catalog_category.currentStore

Set/Get current store view

Return: int

Arguments:

  • mixed storeView - Store view ID or code.

Aliases:

  • category.currentStore

catalog_category.tree

Retrieve hierarchical tree of categories.

Return: array

Arguments:

  • int parentId - parent category id (optional)
  • mixed storeView - store view (optional)

Aliases:

  • category.tree

catalog_category.level

Retrieve one level of categories by website/store view/parent category

Return: array

Arguments:

  • mixed website - website code or Id (optional)
  • mixed storeView - store view code or Id (optional)
  • mixed parentCategory - parent category Id (optional)

Aliases:

  • category.level

catalog_category.info

Retrieve category data

Return: array

Arguments:

  • int $categoryId - category ID
  • mixed $storeView - store view id or code (optional)
  • array $attributes - return only specified attributes (optional)

Aliases:

  • category.info

catalog_category.create

Create new category and return its id.

Return: int

Arguments:

  • int $parentId - ID of parent category
  • array $categoryData - category data ( array(’attribute_code’⇒‘attribute_value’ )
  • mixed $storeView - store view ID or code (optional)

Aliases:

  • category.create

catalog_category.update

Update category

Return: boolean

Arguments:

  • int $categoryId - ID of category for updating
  • array $categoryData - category data ( array(’attribute_code’⇒‘attribute_value’ )
  • mixed storeView - store view ID or code (optional)

Aliases:

  • category.update

catalog_category.move

Move category in tree

Return: boolean

Arguments:

  • int $categoryId - category ID for moving
  • int $parentId - new category parent
  • int $afterId - category ID after what position it will be moved (optional)

Aliases:

  • category.move

NOTE Please make sure that you are not moving category to any of its own children. There are no extra checks to prevent doing it through webservices API, and you won’t be able to fix this from admin interface then

catalog_category.delete

Delete category

Return: boolean

Arguments:

  • int $categoryId - category ID

Aliases:

  • category.delete

catalog_category.assignedProducts

Retrieve list of assigned products

Return: array

Arguments:

  • int $categoryId - category ID
  • mixed $store - store ID or code

Aliases:

  • category.assignedProducts

catalog_category.assignProduct

Assign product to category

Return: boolean

Arguments:

  • int $categoryId - category ID
  • mixed $product - product ID or sku
  • int $position - position of product in category (optional)

Aliases:

  • category.assignProduct

catalog_category.updateProduct

Update assigned product

Return: boolean

Arguments:

  • int $categoryId - category ID
  • mixed $product - product ID or sku
  • int $position - position of product in category (optional)

Aliases:

  • category.updateProduct

catalog_category.removeProduct

Remove product assignment from category

Return: boolean

Arguments:

  • int $categoryId - category ID
  • mixed $product - product ID or sku

Aliases:

  • category.removeProduct

Faults

Fault Code Fault Message
100 Requested store view not found.
101 Requested website not found.
102 Category not exists.
103 Invalid data given. Details in error message.
104 Category not moved. Details in error message.
105 Category not deleted. Details in error message.
106 Requested product is not assigned to category.

Examples

Example 1. Working with categories

  1. function getSomeRandomCategory(&$categories, $targetLevel, $currentLevel = 0) {
  2.     if (count($categories)==0) {
  3.         return false;
  4.     }
  5.     if ($targetLevel == $currentLevel) {
  6.         return $categories[rand(0, count($categories)-1)];
  7.     } else {
  8.         return getSomeRandomCategory($categories[rand(0, count($categories)-1)]['children'], $targetLevel + 1);
  9.     }   
  10. }
  11.  
  12. $proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
  13. $sessionId = $proxy->login('apiUser', 'apiKey');
  14. $allCategories = $proxy->call($sessionId, 'category.tree'); // Get all categories.
  15.  
  16. // select random category from tree
  17. while (($selectedCategory = getSomeRandomCategory($allCategories, 3)) === false) {}
  18.  
  19. // create new category
  20. $newCategoryId = $proxy->call(
  21.     $sessionId,
  22.     'category.create',
  23.     array(
  24.         $selectedCategory['category_id'],
  25.         array('name'=>'New Category Through Soap')
  26.     )
  27. );
  28.  
  29. $newData = array('is_active'=>1);
  30. // update created category on German store view
  31. $proxy->call($sessionId, 'category.update', array($newCategoryId, $newData, 'german'));
  32.  
  33. $firstLevel = $proxy->call($sessionId, 'category.level', array(null, 'german', $selectedCategory['category_id']));
  34.  
  35. var_dump($firstLevel);
  36.  
  37. // If you wish remove category, uncomment next line
  38. //$proxy->call($sessionId, 'category.delete', $newCategoryId);

Example 2. Working with assigned products

  1. $proxy = new SoapClient('http://magentohost/api/soap/?wsdl');
  2. $sessionId = $proxy->login('apiUser', 'apiKey');
  3.  
  4. $categoryId = 5; // Put here your category id
  5. $storeId = 1; // You can add store level
  6.  
  7. $assignedProducts = $proxy->call($sessionId, 'category.assignedProducts', array($categoryId, $storeId));
  8. var_dump($assignedProducts); // Will output assigned products.
  9.  
  10. // Assign product
  11. $proxy->call($sessionId, 'category.assignProduct', array($categoryId, 'someProductSku', 5));
  12.  
  13. // Update product assignment postion
  14. $proxy->call($sessionId, 'category.updateProduct', array($categoryId, 'someProductSku', 25));
  15.  
  16. // Remove product assignment
  17. $proxy->call($sessionId, 'category.removeProduct', array($categoryId, 'someProductSku'));



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs