Magento bietet von Haus aus Multi-Select Attribute an, mit welchen einem Produkt mehrere Werte aus vorgegebenen Optionen zugewiesen werden können. Über das Backend können diese problemlos angelegt und bearbeitet werden. Möchte man die Attribute aber automatische durch eine Routine generieren lassen und mit Werten belegen stößt man schnell an die Grenzen der API-Funktionen von Magento.
Im folgenden Artikel wird der komplette Workflow gezeigt, um
- solche Attribute programmatisch anzulegen
- die Attribute einem Attribut-Set zuzuweisen
- Attribut-Optionen programmatisch hinzuzufügen
- den jeweiligen Produkten Werte zuzuweisen
Multi-Select Attribut erstellen
In diesem Artikel wird davon ausgegangen, dass das Attribut noch nicht in Magento vorhanden ist und über eine Routine angelegt werden soll. Hierzu steht die API-Methode catalog_product_attribute.create zur Verfügung, mit welcher ein Attrribut mit all seinen Eigenschaften erstellt werden kann. Als Beispiel legen wir das Attribut “multi-attribute” an:
$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'
)
));
Attribute einer Attributset-Gruppe zuweisen
Hat man das Attribut angelegt, muss es einer Attribut-Set Gruppe zugewiesen werden. Ansonsten wird das Attribut im Backend nicht angezeigt und auch über die API können dem Produkt keine Werte für das Attribut hinzugefügt werden.
$proxy->call($sessionId, 'catalog_product_attribute_set.addAttributeToGroup', array(
'4',
'General',
'multi-attribute'
));
Attribut-Optionen hinzufügen
Nun ist das Attribut in den Produkten die dem jeweiligen Attribut-Set zugeordnet sind zwar sichtbar, aber es besitzt noch keinerlei Attribut-Optionen. Damit kann im Backend vorerst nichts ausgewählt werden. Darum fügen wir nun die Attribut-Optionen hinzu.
Hierbei ist es wichtig, die Ids der Optionen zwischenzuspeichern, da diese im nächsten Schritt benötigt werden.
$optionIds = array();
foreach (array('Wert 1','Wert 2','Wert 3','Wert 4') as $opt) {
$optionIds[] = $proxy->call($sessionId, 'catalog_product_attribute_option.create',array(
'multi-attribute',
$opt
));
}
Multi-Select Attributwerte einem konkreten Produkt zuweisen
Das Multi-Select Attribut ist nun entsprechend auf seine Verwendung vorbereitet. Wir können uns nun also den Produkten zuwenden und die entsprechenden Werte setzen. Hierbei kommen wir auf die im vorherigen Schritt gemerkten Options-Ids zurück, welche zum setzen der Attribut-Werte benötigt werden. Hier ist es leider nicht möglich die konkreten Options-Werte zu setzen, sondern werden deren Ids benötigt.
Im Beispiel machen wir es uns einfach und setzen einfach alle Attribut-Optionen für das Produkt als ausgewählt:
$proxy->call($sessionId, 'catalog_product.update', array(
'test1234',
array(
'multi-test' => $optionIds
)
));
Damit haben wir nun erfolgreich einem Produkt ein Multi Select-Attribut hinzugefügt und die entsprechenden Werte gesetzt.
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.