
The other day, the task was received to make the import of goods in Prestashop. I have not dealt with this CMS before, and therefore I began to look for familiar APIs for adding / changing products. What was my surprise when I did not find them, well, or not looking very much. The solution described
here did not fit unambiguously, since it required working with combinations of the product, and a bunch of other parameters. I note that the option of changing the kernel files or creating heaps of files overriding the system classes was dropped immediately. And here I paid attention to the REST API, as I understood it appeared recently, and there is little information on it, but it seemed to me to be the best option. I note that in this material I do not set a goal to repeat what is described in the documentation, but only to streamline the information and add some points.
Activation of the REST interface in the store
The activation is described in some detail on the
website with documentation , so we will not particularly linger on this moment. The main thing is not to forget to activate the CNC, otherwise it will not work. After activation, you can check the operation of the API by following
yoursite.ru/api/ .
Basic concept
Communication with the store is entirely in XML, except for the case of loading images, which we consider below. The API system is built on the CRUD principle (Create, Read, Update, Delete) which translates into HTTP request language and gives us the following:

Most of the communication with the store will occur through the
PSWebServiceLibrary class written by one of the community members. You can download it by reference, or find on the pages of the
documentation .
A list of all possible methods, and an explanation of the types of variables lies here at
doc.prestashop.com , in fact, this is the most necessary document from all the documentation, however, following the link
yoursite.ru/api/ and entering the previously created access key as a login you can look like the links indicated in the attribute "xlink: href", and visually see the structure of the API.
Creating items
To create any element of the store structure (hereinafter, take the product as an example), we first need to obtain an XML document procurement. For this to link to the list of products you need to add? Schema = synopsis. Below is an example of creating a product through the PSWebServiceLibrary class:
In the $ created_product variable, we have an XML product card in case of success, or a card with a description of the error. This statement is true for creating any items except pictures.
If you look at the debug output, you can see that the class climbs behind the blank at
yoursite.ru/api/products/?schema=synopsis using the GET method and adds the product using the POST method to
yoursite.ru/api/products/ .
')
Creating pictures
Unfortunately, there is no ready method for uploading pictures in the PSWebServiceLibrary class, so I had to pin my own. For good it should be added to the above class, but this was neither the strength nor the time. Therefore, we will upload pictures ourselves. Also, the URL for uploading pictures is not just / api / images / as for example in the case of products (/ api / products /) but if it is a product picture, / api / images / products / $ product_id /. This feature was identified empirically.
Code download pictures for the goods:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $shop_url."api/images/products/".$product_id."/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERPWD, $secret_key.":"); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => '@my_image.png')); $response= curl_exec($ch);
In the $ response variable, we will end up with either the image we downloaded, or the XML error card. Here you need to check for a download error by checking the server response code, or the response text.
It should be noted that it is impossible to get the id of the uploaded image, for this I immediately after loading the image did a selection of all the images of the specified product (/ api / images / products / $ product_id /) and the one with the largest id is ours.
Editing items
Editing is almost the same as creating, except for the fact that at the beginning we get not the product card blank, but the product itself. After modifying the fields, we simply change the PSWebServiceLibrary class method from add to edit with the same parameters.
$product = $webService->get(array('resource' => 'products', 'id' => $product_id)); $mod_product = $webService->edit(array('resource' => 'products', 'id' => $product_id, 'putXml' => $product->asXML()));
Deleting items
Deletion takes place in syntax similar to the get method, but only using the delete method.
$webService->delete(array('resource' => 'products', 'id' => $product_id));
In response, nothing will be returned.
Catching errors
If we use the PSWebServiceLibrary class, then each call to its methods must be wrapped in a try ... catch block, as if there is a server error, it will generate a Fatal error of the type PrestaShopWebserviceException. More details below:
try {
Filters, sorting, limits
About all of the above is pretty well written
in the documentation . And it makes no sense to repeat it here. Just do not forget that this opportunity exists. The filter can be specified for any field from the XML card, specifying the name of the tag containing the value for the filter.
Underwater rocks
Carefully check the value for compliance with the requirements of the field type, one discrepancy in a small field will lead to the impossibility of performing a whole chain of actions. All field types and regular expressions to check for compliance are here
doc.prestashop.com .
PS: If anyone has any questions, I’ll be happy to answer and supplement the material.