This example was made using Prestashop: 1.7.6.5.
I’ve decided to write this tutorial, since I’ve had trouble getting to the right solution:
<?php
require_once 'PSWebServiceLibrary.php';
$url = 'myurl.com';
$key = 'ABCDEFGHIJKLMNOPQRSTUVXZ1234567890';
$debug = true;
$webService = new PrestaShopWebservice($url, $key, $debug);
$searchTerm = "YOURPRODUCTREFERENCE";
$xml = $webService->get([
'resource' => 'products',
'display' => 'full',
'filter[reference]' => $searchTerm
]);
$resource = $xml->products->children()->children();
This search has the objective to get the resulting ID (or ID’s, but in this case I’m expecting to have only one).
The response will be something like this:
HTTP RESPONSE HEADER
HTTP/1.1 200 OK
Date: Wed, 22 Jul 2020 13:44:54 GMT
Server: Apache
Access-Time: 1595425494
X-Powered-By: PrestaShop Webservice
PSWS-Version: 1.7.6.5
Execution-Time: 0.016
Content-Sha1: a2551cfed7afc50cbbf86a25881a0aa18b39d078
Vary: Authorization
Upgrade: h2,h2c
Connection: Upgrade
Transfer-Encoding: chunked
Content-Type: text/xml;charset=utf-8
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>
Now we have the ID and the object ready to update… WRONG! I’ve not found a good way to update the object with the data enclosed in the tags
<products><product></product></products>
Now you have to GET the product data to a new object, like this:
$xml = $webService->get([
'resource' => 'products',
'id' => $resource->id,
]);
$productFields = $xml->children()->children();
The result is now like this:
RETURN HTTP BODY
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<product>
<id><![CDATA[21346]]></id>
Notice that now we have only the <product> tag. Now we can update the object. But first, let’s remove the read only fields, to avoid further errors:
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);
And the rest of the code, like this:
// setting the new price
$productFields->price = (int) "666.00";
// update
$updatedXml = $webService->edit([
'resource' => 'products',
'id' => (int) $productFields->id,
'putXml' => $xml->asXML(),
]);
Hope that this helps anyone in need like I was. If you have any suggestions or other solution, please leave it in the comment box below. Thanks.