Zarządzanie atrybutami
Przykład wywołania w PHP
<?php /* * Dodanie grupy atrybutów, atrybutów i przypisanie wartości atrybutów do produktu */ /** * Logowanie do API * * @param resource $c cURL resource handle * @param string $login Login użytkownika * @param string $password Hasło użytkownika * @return string Indentyfikatorr sesji użytkownika */ function login($c, $login, $password) { $params = Array( "method" => "login", "params" => Array($login, $password) ); curl_setopt($c, CURLOPT_POSTFIELDS, "json=" . json_encode($params)); $result = (Array) json_decode(curl_exec($c)); if (isset($result['error'])) { return null; } else { return $result[0]; } } $c = curl_init(); curl_setopt($c, CURLOPT_URL, 'http://shop.example.com/webapi/json/'); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // zalogowanie użytkownika i pobranie identyfikatora sesji $session = login($c, "api", "test"); if ($session != null) { // ------------------------------------------------------------------------- // 1. Dodanie grupy atrybutów $atttributeGroup = Array( "name" => "Kosmetyki", "lang_id" => 1, // id języka (polski) "active" => 1, "categories" => Array(6), // id kategorii ); $params = Array( "method" => "call", "params" => Array($session, "attribute.group.create", Array($atttributeGroup)) ); // zakodowanie parametrów dla metody POST $postParams = "json=" . json_encode($params); curl_setopt($c, CURLOPT_POSTFIELDS, $postParams); // dekodowanie rezultatu w formacie JSON do tablicy result $data = curl_exec($c); $result = (Array)json_decode($data); // sprawdzenie, czy wystąpił błąd if (isset($result['error'])) { echo "Wystąpił błąd: " . $result['error'] . ", kod: " . $result['code']; } else { if ($result[0] > 0) { $attributeGroupId = $result[0]; } else { echo "Błąd dodawania grupy atrybutów"; } } // ------------------------------------------------------------------------- // 2. Dodanie atrybutów do stworzonej grupy $atttribute1 = Array( "name" => "Typ", "description" => "Typ kosmetyku", "pres_id" => $attributeGroupId, "order" => 1, "type" => 2, // pole select "active" => 1, "default" => null, "options" => Array("Balsam", "Szampon", "Woda toaletowa"), ); $atttribute2 = Array( "name" => "Pojemność", "description" => "Pojemność kosmetyku", "pres_id" => $attributeGroupId, "order" => 2, "type" => 0, // pole tekstowe "active" => 1, "default" => null, "options" => null, ); $atttributes = Array( $atttribute1, $atttribute2 ); $params = Array( "method" => "call", "params" => Array($session, "attribute.list.create", Array($atttributes)) ); // zakodowanie parametrów dla metody POST $postParams = "json=" . json_encode($params); curl_setopt($c, CURLOPT_POSTFIELDS, $postParams); // dekodowanie rezultatu w formacie JSON do tablicy result $data = curl_exec($c); $result = (Array)json_decode($data); // sprawdzenie, czy wystąpił błąd if (isset($result['error'])) { echo "Wystąpił błąd: " . $result['error'] . ", kod: " . $result['code']; } else { if ($result[0] > 0) { $attId1 = $result[0]; } else { echo "Błąd dodawania atrybutu 1"; } if ($result[1] > 0) { $attId2 = $result[1]; } else { echo "Błąd dodawania atrybutu 2"; } } // ------------------------------------------------------------------------- // 3. Przypisanie wartości atrybutów dla produktu $product = Array( "attributes" => Array( $attId1 => "Woda toaletowa", $attId2 => "220ml", ) ); $params = Array( "method" => "call", "params" => Array($session, "product.save", Array(67, $product, true) // id produktu, dane, force ) ); // zakodowanie parametrów dla metody POST $postParams = "json=" . json_encode($params); curl_setopt($c, CURLOPT_POSTFIELDS, $postParams); // dekodowanie rezultatu w formacie JSON do tablicy result $data = curl_exec($c); $result = (Array)json_decode($data); // sprawdzenie, czy wystąpił błąd if (isset($result['error'])) { echo "Wystąpił błąd: " . $result['error'] . ", kod: " . $result['code']; } else { if ($result[0] == -1) { echo "Podane dane są nieprawidłowe i nie spełniają wymagań walidacji"; } else if ($result[0] == 0) { echo "Operacja się nie udała"; } else if ($result[0] == 1) { echo "Produkt został zapisany"; } else if ($result[0] == 2) { echo "Operacja się nie udała - obiekt jest zablokowany przez innego administratora"; } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>