Add Multi Select Attributes via SOAP/XML-RPC using Magento Core API Extension

Magento provides the ability to add multi-select attributes. With this functionality is is possible to link several predefined options out of a range to a product. Via the Magento backend this can be achieved easily by selecting the options and saving the product. Generating the options automatically using a API based routine lets you hit the limits of the Magento Core API functionality.

In the following the complete workflow is shown to

  • add a multi select attribute
  • add the attribute to an attribute set
  • add attribut options automatically
  • and set the created options for a product

Add a multi select attribute

In this article we assume that the attribut has not been already created in the backend but shoudl be added via a routine. To do this this there is the method catalog_product_attribute.create available. Using this method you can create an attribute of any kind with all its properties. In our example we create an attribute called “multi-attribute”:

$attributeId = $proxy->call($sessionId, 'catalog_product_attribute.create', array(
    'multi-attribute',
    array(
        'frontend_label'            => 'Multi-Attribute',
        'frontend_input'            => 'multiselect',
        'is_filterable'             => 1,
        'is_filterable_in_search'   => 1,
        'is_configurable'           => 1,
        'apply_to'                  => 'simple'
    )
));

Add the attribute to an attribute set

If the attribute has been created it needs to be added to an attribute set group in order to use it. If not added to an attribute set the attribute will not be shown in Magento backend and it is also not possible to use it via the api.

$proxy->call($sessionId, 'catalog_product_attribute_set.addAttributeToGroup', array(
    '4',
    'General',
    'multi-attribute'
));

Add attribute options

Now the attribute has been added to a group and is visible in the products of this attribute set, but there are no options available to select. This way it is not possible to select anything in the backend for this attribute. Because we want to use the attribute, we just add some options to it in the next step.
While adding the options it is important to remember the option ids for later use.

$optionIds = array();
foreach (array('Value 1','Value 2','Value 3','Value 4') as $opt) {
    $optionIds[] = $proxy->call($sessionId, 'catalog_product_attribute_option.create',array(
      'multi-attribute',
      $opt
    ));
}

Add multi select attribute option values to a product

The multi select attribute is now ready to use. That means we are now able to take a specific product and set values to it. Now we also see why it was important to rembemer the option ids in the last step. Unfortunately it is not possible to take the option values and set them to the product – Magento needs the ids of the created options to link them to the product.

In our example we just take all option ids and set them completely to the product:

$proxy->call($sessionId, 'catalog_product.update', array(
    'test1234',
    array(
        'multi-test'    => $optionIds
    )
));

Now we are finished – we have a product with options set to a newly created multi select attribute. That’s it.

Gepostet am 28.02.2012
von Dominik Krebs