Sie möchten ein konfigurierbares Produkt über die API zu Magento hinzufügen? Folgendes Tutorial soll Ihnen hierbei behilflich sein.
Configurable Product anlegen
Um ein Configurable Product anzulegen kann die catalog_product.create-Methode verwendet werden. Der erste Parameter bestimmt hier, welcher Typ von Artikel angelegt wird. In diesem Fall wäre hier ‘configurable’ zu verwenden.
$proxy->call($sessionId, 'catalog_product.create', array( 'configurable', '4', // setId 'configurable_sku', array( 'name' => 'Product name', 'description' => 'Description Text...', 'short_description' => 'Short Description Text...', 'websites' => array(1), 'price' => 100.00 ) );
Simple Product anlegen
Als nächstes müssen die Simple Products angelegt werden, die die Varianten des Configurable Products darstellen. Jedes Simple Product entspricht somit einer Attribut-Kombination des Configurable Products und kann auch auf diese Weise später auf der Produktseite durch Auswahl der entsprechenden Optionen gekauft werden.
Optionen anlegen bzw. Id’s ermitteln
Jedes Simple Product benötigt die entsprechenden Optionen, über die es ausgewählt werden kann. In diesem Beispiel nehmen wir an, wir erstellen ein Produkt bei welchem Größe und Farbe auswählbar sein soll. Es müssen also die Größen (z.B. S, M, L, XL, XXL) sowie die Farben (z.B. grün, gelb, rot, braun, schwarz) im jeweiligen Select-Attribut existieren.
Über die API können diese wie folgt angelegt werden:
$options = $proxy->call($sessionId, 'catalog_product_attribute.options', array('23'));
foreach ($options as $opt) {
if ($opt['label'] == 'yellow') {
$color = $opt['value'];
}
}
if (!isset($color)) {
$color = $proxy->call($sessionId, 'catalog_product_attribute.addOption', array('23','yellow'));
}
$options = $proxy->call($sessionId, 'catalog_product_attribute.options', array('24'));
foreach ($options as $opt) {
if ($opt['label'] == 'XL') {
$size = $opt['value'];
}
}
if (!isset($size)) {
$size = $proxy->call($sessionId, 'catalog_product_attribute.addOption', array('24','XL'));
}
Sind die Optionen entsprechend angelegt oder die Id der jeweiligen Option ermittelt, können die Simple Products, also die eigentlichen Varianten mit den jeweiligen Attributkombinationen angelegt werden:
$proxy->call($sessionId, 'catalog_product.create', array( 'simple', '4', // setId 'simple_sku_yellow_XL', array( 'name' => 'Product name', 'description' => 'Description Text...', 'short_description' => 'Short Description Text...', 'websites' => array(1), 'price' => 100.00, 'color' => $color, 'size' => $size, 'visibility' => 1, ) );
Zu beachten beim Simple Product ist, dass es in der Regel für den Kunden nicht als Produkt im Shop sichtbar ist. Um das zu Erreichen muss die Sichtbarkeit auf “Nirgendwo” gestellt werden. Damit ist das Simple Product nur über sein Configurable Product kaufbar und wird weder in Katalog noch Suche angezeigt.
Verknüpfen der Artikel
Als letzter Schritt werden die Artikel miteinander verknüpft – es werden die Simple Products, also die Varianten mit dem Configurable Product zusammengefügt. Erst nach diesem Schritt weiss das Configurable Product überhaupt von seinen Optionen.
Dabei können, wie über das Webinterface auch, alle verfügbaren Optionen angepasst werden:
$proxy->call($sessionId, 'catalog_product_type_configurable.assign', array(
$configurableProductIdOrSku,
$simpleProductIdsOrSkus,
array('color','size'),
array('color'=>'Farbe','size'=>'Grösse'),
array(
'gelb' => array(
'pricing_value' =>'35',
'is_percent' =>0
),
'XL' => array(
'pricing_value' =>20.00,
'is_percent' =>1
)
)
));
Die Parameter im Einzelnen:
- configurableProductId: die Id oder SKU des konfigurierbaren Produktes
- simpleProducts: ein Array aus Id’s oder SKU’s der zugehörigen Simple Products
- usedAttributes: die Attribute, welche verwendet werden sollen um das konfigurierbare Produkt zu erstellen
- labels: ein Key-Value-Pair aus Label und Attribut-Code, um den Titel zu bestimmen, welcher auf der Produktseite angezeigt wird
- prices: Array mit Preisen pro Attribut-Option, entweder als absoluter Preis oder in Prozent
- store: Store für den die Attribut-Labels definiert werden. So können unterschiedliche Sprachen gepflegt werden
In case you want to add a Configurable Product using Magento Core API, the following tutorial should help you to deal with this.
Create a Configurable Product
To create a Configurable Product you may use the catalog_product.create method. The type of product can be specified with the first paramter. In this case you may have to use ‘configurable’.
$proxy->call($sessionId, 'catalog_product.create', array( 'configurable', '4', // setId 'configurable_sku', array( 'name' => 'Product name', 'description' => 'Description Text...', 'short_description' => 'Short Description Text...', 'websites' => array(1), 'price' => 100.00 ) );
Create a Simple Product
Now you have to create the Simple Products – the variants of the Configurable Product. Every Simple Product corresponds to an attribute combination of the Configurable Product and may be bought by customers by choosing the appropriate options on the product page.
Create options
Every simple product needs to have its options which can be selected by customers.
Example: we want to create a product which should have the attributes ‘color’ and ‘size’. So we need different sizes (e.g. S, M, L, XL, XXL) as well as different colors (e.g. green, yellow, red, brown, black) in the corresponding select-attribute.
You may create these options using the API like this:
$options = $proxy->call($sessionId, 'catalog_product_attribute.options', array('23'));
foreach ($options as $opt) {
if ($opt['label'] == 'yellow') {
$color = $opt['value'];
}
}
if (!isset($color)) {
$color = $proxy->call($sessionId, 'catalog_product_attribute.addOption', array('23','yellow'));
}
$options = $proxy->call($sessionId, 'catalog_product_attribute.options', array('24'));
foreach ($options as $opt) {
if ($opt['label'] == 'XL') {
$size = $opt['value'];
}
}
if (!isset($size)) {
$size = $proxy->call($sessionId, 'catalog_product_attribute.addOption', array('24','XL'));
}
If you have created these options its easy to find out the id of the option which is necessary to assign the option to the Simple Products.
$proxy->call($sessionId, 'catalog_product.create', array( 'simple', '4', // setId 'simple_sku_yellow_XL', array( 'name' => 'Product name', 'description' => 'Description Text...', 'short_description' => 'Short Description Text...', 'websites' => array(1), 'price' => 100.00, 'color' => $color, 'size' => $size, 'visibility' => 1, ) );
Putting all together
In the last step you may link all products so they can be bought via the Configurable Product. Using our API call you may specify all options which can be also specified manually at the web interface.
$proxy->call($sessionId, 'catalog_product_type_configurable.assign', array(
$configurableProductIdOrSku,
$simpleProductIdsOrSkus,
array('color','size'),
array('color'=>'Farbe','size'=>'Grösse'),
array(
'gelb' => array(
'pricing_value' =>'35',
'is_percent' =>0
),
'XL' => array(
'pricing_value' =>20.00,
'is_percent' =>1
)
)
));
A short explanation of all options:
- configurableProductId: Id or sku of Configurable Product
- simpleProducts: an array which contains Ids or skus of all Simple Products which should be linked
- usedAttributes: attributes which should be used to create the Configurable Product
- labels: a key-value-pair including label and attribute code, to specifiy the title which will be shown at the product page for each attribute
- prices: an array containing prices per attribut option (as absolut price or percent value)
- store: by specifying the store you can change the attribute names per store (different languages)
