Extend Magento Product Attribute Api

# OPTIONAL, install patch
apt-get install -y patch

# patch on command line
cd /var/www/
wget http://www.panticz.de/sites/default/files/api.xml_.diff -O /tmp/api.xml.diff
patch -p2 ./app/code/core/Mage/Catalog/etc/api.xml < /tmp/api.xml.diff

wget http://www.panticz.de/sites/default/files/Api.php_.diff -O /tmp/Api.php.diff
patch -p2 ./app/code/core/Mage/Catalog/Model/Product/Attribute/Api.php < /tmp/Api.php.diff

1. go to magento installation folder, for example /var/www/magento

2. add definition to app/code/core/Mage/Catalog/etc/api.xml (line 215)

Create new product attribute
catalog/product/attribute/create

Delete product attribute
catalog/product/attribute/delete

Add attribute options
catalog/product/attribute/addoptions

3. add functions to app/code/core/Mage/Catalog/Model/Product/Attribute/Api.php
/**
* Create new product attribute.
*
* @param string $attributeName
* @param array $attributeData
* @param string|int $store
* @return int
*/
public function create($attributeName, $attributeData, $store = null)
{
// create product attribute
$installer = new Mage_Catalog_Model_Resource_Eav_Mysql4_Setup('core_setup');
$installer->addAttribute('catalog_product', $attributeName, $attributeData);

// get product attribute id
$storeId = $this->_getStoreId($store);
$attribute = Mage::getModel('catalog/product')
->setStoreId($storeId)
->getResource()
->getAttribute($attributeName);

return $attribute->getId();
}

/**
* Create attribute options
*
* @param string $attributeId
* @param array $attributeOptions
* @return int
*/
public function addoptions($attributeId, $attributeOptions)
{
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

for($i = 0; $i < sizeof($attributeOptions); $i++) {
$option = array();
$option['attribute_id'] = $attributeId;
$option['value'][$value][0] = $attributeOptions[$i];

$setup->addAttributeOption($option);
}

return true;
}

/**
* Delete product attribute.
*
* @param string $attributeName
* @param string|int $store
* @return int
*/
public function delete($attributeName, $store = null)
{
$storeId = $this->_getStoreId($store);
$attribute = Mage::getModel('catalog/product')
->setStoreId($storeId)
->getResource()
->getAttribute($attributeName);

if (!$attribute) {
$this->_fault('not_exists');
}

try {
$attribute->delete();
} catch (Mage_Core_Exception $e) {
$this->_fault('not_deleted', $e->getMessage());

return false;
}

return true;
}

Extention for MD5 sum
http://www.panticz.de/sites/default/files/magento/Api/media.md5.api.php.diff
http://www.panticz.de/sites/default/files/magento/Api/media.md5.api.xml.diff

# Links
http://www.magentocommerce.com/wiki/doc/webservices-api/custom-api
http://svn.magentocommerce.com/source/branches/1.4-trunk/app/code/core/Mage/Catalog/etc/api.xml
http://svn.magentocommerce.com/source/branches/1.4-trunk/app/code/core/Mage/Catalog/Model/Product/Attribute/Api.php
http://www.magentocommerce.com/wiki/doc/installing_custom_attributes_with_your_module
http://snippi.net/node?page=1

/var/www/magento/app/code/core/Mage/Eav/Model/Entity/Setup.php
/var/www/magento/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Setup.php
/var/www/magento/app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php