'Prestashop API: edit product price
Using: Prestashop: 1.7.6.5.
I'm trying to create a piece of code to update products prices (or other information in products) with the code below, but it always results in the error "id is required when modifying a resource".
The code:
<?
require_once 'PSWebServiceLibrary.php';
$url = 'myurl.com';
$key = 'ABCDEFGHIJKLMNOPQRSTUVXZ1234567890';
$debug = true;
$searchTerm = "ZXCVBNM";
try {
$webService = new PrestaShopWebservice($url, $key, $debug);
$xml = $webService->get([
'resource' => 'products',
'display' => 'full',
'filter[reference]' => $searchTerm
]);
} catch (PrestaShopWebserviceException $ex) {
echo 'Other error: <br />' . $ex->getMessage();
}
$productFields = $xml->products->children()->children();
unset($productFields->manufacturer_name);
unset($productFields->quantity);
unset($productFields->id_shop_default);
unset($productFields->id_default_image);
unset($productFields->associations);
unset($productFields->id_default_combination);
unset($productFields->position_in_category);
unset($productFields->type);
unset($productFields->pack_stock_type);
unset($productFields->date_add);
unset($productFields->date_upd);
$productFields->price = (int) "666.00";
$updatedXml = $webService->edit([
'resource' => 'products',
'id' => (int) $productFields->id,
'putXml' => $xml->asXML(),
]);
The GET result is:
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
<id><![CDATA[21346]]></id>
...
</product>
</products>
</prestashop>
The PUT:
XML SENT
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
<id>21346</id>
...
<price>666</price>
...
</product>
</products>
</prestashop>
It returns:
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<errors>
<error>
<code><![CDATA[90]]></code>
<message><![CDATA[id is required when modifying a resource]]></message>
</error>
</errors>
</prestashop>
Can't figure out what is wrong.
Solution 1:[1]
I've figured out that searching by reference, the resulting object data if formatted to display multiple products, and I think that in this way update it's not available.
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<products>
<product>
...
<product>
<products>
After this result one must iterate through the resulting IDs.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jaclerigo |