Magento

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

Icon NetzkollektivBy Dominik Krebs, Managing Director, CTO

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”:

[php]
$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’
)
));
[/php]

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.

[php]
$proxy->call($sessionId, ‘catalog_product_attribute_set.addAttributeToGroup’, array(
‘4’,
‘General’,
‘multi-attribute’
));
[/php]

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.

[php]
$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
));
}
[/php]

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:

[php]
$proxy->call($sessionId, ‘catalog_product.update’, array(
‘test1234’,
array(
‘multi-test’ => $optionIds
)
));
[/php]

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

Magento Agentur gesucht?

Sie sind auf der Suche nach einer zuverlässigen Agentur, die sich mit Magento auskennt und Ihnen unkompliziert durch den Online-Shop Alltag hilft? NETZKOLLEKTIV betreut Händler seit über 10 Jahren. Rufen Sie uns an!

Jetzt unverbindlich über mein Projekt sprechen