Update only specified fields of PrestaShop product via API

Viewed 695

I would like to know if there is a way to update a ressource (product in this case), but only update fields that are specified in my request.

For example, let's say I want to update only the required fields for the product, but not change the others, how could I proceed via the webservice ?

As far as I know, when you update a ressource, it will update it as whole : This means if you don't "re-send" the original value back when updating (if you do not specify ALL the fields), it is considered as empty.

From my tests, I tried the following :

  1. Log in my PrestaShop, get a random product, set the "width" to a random value (let's say 30 cm)
  2. Retrieve the blank schema of a product, and fill only the required fields of the schema (width is not part of it)
  3. Send the schema via the webservice to update the original product using a PUT request
  4. Get back in my PrestaShop and notice in despair and sadness that the width value has been set to 0

My use case is that we have a system that is synching products with PrestaShop. When a product is edited in our system, a specific set of fields is sent back to PrestaShop so the product in the shop is also updated.

But for some of our users, they want to be able to add information on the shop and keep them even if the product is updated afterwards. For example, they add dimensions to the product (fields that are not managed / persisted in our system) and they want to keep those information.

The constraint we have is that the set of fields sent for updates is "hardcoded" : We can't get the ressource schema and update it to send it back afterwards.

Is there any parameter / configuration that can be set so values of fields that are not specified are not erased ?

2 Answers

Due to the nature of how the PrestaShop API works, you can perform the following:

For each product you want to update, you can pull the complete object (with all fields), then update that object with only the fields you need updated, and then push the whole object as n update for that product via the API.

Doing this for multiple products will require twice the API calls.. but this is one workaround.

One way is to use a flag variable for the properties which you want to update

Related