Zarządzanie wariantami
Przykład wywołania w PHP
<?php /* * Dodanie grupy cech, cech i stworzenie wariantów dla 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 cech $optionGroup = Array( "translations" => Array( "pl_PL" => Array( "name" => "Rozmiary i kolory" ) ) ); $params = Array( "method" => "call", "params" => Array($session, "option.group.create", Array($optionGroup)) ); // 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) { $optionGroupId = $result[0]; } else { echo "Błąd dodawania grupy cech"; } } // ------------------------------------------------------------------------- // 2. Dodanie cech $option1 = Array( "translations" => Array( "pl_PL" => Array( "name" => "Rozmiar" ) ), "group_id" => $optionGroupId ); $option2 = Array( "translations" => Array( "pl_PL" => Array( "name" => "Kolor" ) ), "group_id" => $optionGroupId ); $options = Array( $option1, $option2 ); $params = Array( "method" => "call", "params" => Array($session, "option.list.create", Array($options)) ); // 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) { $optionId1 = $result[0]; } else { echo "Błąd dodawania cechy 1"; } if ($result[1] > 0) { $optionId2 = $result[1]; } else { echo "Błąd dodawania cechy 2"; } } // ------------------------------------------------------------------------- // 3. Dodanie wartości cech $optionValue1 = Array( "translations" => Array( "pl_PL" => Array( "value" => "S" ) ), "option_id" => $optionId1 ); $optionValue2 = Array( "translations" => Array( "pl_PL" => Array( "value" => "XL" ) ), "option_id" => $optionId1 ); $optionValue3 = Array( "translations" => Array( "pl_PL" => Array( "value" => "czerwony" ) ), "option_id" => $optionId2 ); $optionValue4 = Array( "translations" => Array( "pl_PL" => Array( "value" => "zielony" ) ), "option_id" => $optionId2 ); $optionsValues = Array( $optionValue1, $optionValue2, $optionValue3, $optionValue4 ); $params = Array( "method" => "call", "params" => Array($session, "option.value.list.create", Array($optionsValues)) ); // 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) { $optionValueId1 = $result[0]; } else { echo "Błąd dodawania wartości cechy 1"; } if ($result[1] > 0) { $optionValueId2 = $result[1]; } else { echo "Błąd dodawania wartości cechy 2"; } if ($result[2] > 0) { $optionValueId3 = $result[2]; } else { echo "Błąd dodawania wartości cechy 3"; } if ($result[3] > 0) { $optionValueId4 = $result[3]; } else { echo "Błąd dodawania wartości cechy 4"; } } // ------------------------------------------------------------------------- // 4. Stworzenie wariantów dla produktu $options = Array( Array( "product_id" => 5, "price" => 1.50, "price_type" => 2, // dodanie ceny "stock" => 9, "warn_level" => 2, "weight" => 1.3, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id cechy => id wartości $optionId1 => $optionValueId1, $optionId2 => $optionValueId3, ) ), Array( "product_id" => 5, "price" => 1.50, "price_type" => 2, // dodanie ceny "stock" => 9, "warn_level" => 2, "weight" => 1.3, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id cechy => id wartości $optionId1 => $optionValueId2, $optionId2 => $optionValueId3, ) ), Array( "product_id" => 5, "price" => 1.50, "price_type" => 2, // dodanie ceny "stock" => 9, "warn_level" => 2, "weight" => 1.3, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id cechy => id wartości $optionId1 => $optionValueId1, $optionId2 => $optionValueId4, ) ), Array( "product_id" => 5, "price" => 1.50, "price_type" => 2, // dodanie ceny "stock" => 9, "warn_level" => 2, "weight" => 1.3, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id cechy => id wartości $optionId1 => $optionValueId2, $optionId2 => $optionValueId4, ) ), ); $params = Array( "method" => "call", "params" => Array($session, "product.option.list.create", Array($options)) ); // 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 { foreach ($result as $r) { if ($r == 0) { echo "Operacja się nie udała" . "<br>"; } else if ($r == -1) { echo "Podane dane są nieprawidłowe i nie spełniają wymagań walidacji" . "<br>"; } else if ($r == -2) { echo "Operacja się nie udała - zabrakło czasu na zrealizowanie operacji" . "<br>"; } else { echo "Wariant został dodany, id: " . $r . "<br>"; } } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>