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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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" ?> < 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:
1 2 3 4 5 6 | $xml = $webService ->get([ 'resource' => 'products' , 'id' => $resource ->id, ]); $productFields = $xml ->children()->children(); |
The result is now like this:
1 2 3 4 5 6 | RETURN HTTP BODY <? xml version = "1.0" encoding = "UTF-8" ?> < 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:
1 2 3 4 5 6 7 8 9 10 11 | 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:
1 2 3 4 5 6 7 8 9 | // 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.