Update instock via REST (DE/EN)

Hallo,

ich habe das Problem, dass Shopware, wenn ich nur ein Mengenupdate machen möchte, automatisch auch nach weiteren Feldern fragt. Gibt es eine Möglichkeit, einfach mit einem JSON string wie “{“mainDetail”:{“inStock”:5}}” ein Lagerupdate zu machen? Immer den kompletten Artikel neu nochzuladen scheint mir etwas überzogen, nur um die Lagermenge anzupassen.

Über jede Hilfe würde ich mich freuen

___

Hello,

while trying to update the stock of articles via REST, I stumbled upon the following problem: If I just upload the relevent Data, ie. “{“mainDetail”:{“inStock”:5}}”, Shopware always asks for more information about the article. There must ba another way to simply update the stock without uploading the whole article again, right?

Id love to hear some suggestions about this

Dein UseCase ist eigentlich direkt in den DevDocs genannt: REST API - Examples using the article resource

Ein kurzer Test war bei mir auch erfolgreich - bist Du sicher dass Du ein PUT und keinen POST machst? Sollte es immer noch nicht funktionieren baue am besten ein kleines Code-Beispiel anhand des ApiClients (REST API - Basics).

@hsoebbing schrieb:

Dein UseCase ist eigentlich direkt in den DevDocs genannt: https://developers.shopware.com/developers-guide/rest-api/examples/article/#step-2-update-the-created-article

Ein kurzer Test war bei mir auch erfolgreich - bist Du sicher dass Du ein PUT und keinen POST machst? Sollte es immer noch nicht funktionieren baue am besten ein kleines Code-Beispiel anhand des ApiClients (https://developers.shopware.com/developers-guide/rest-api/#using-the-rest-api-in-your-own-application).

Erstmal danke für die Antwort. Der usecase in den Docs ist genau das, was mich so irritiert. Ich mache einen Put mit 

{„mainDetail“:{„inStock“:98}}

an z.B. articles/13?useNumberAsId=true

und bekomme die Info, dass informationen fehlen. Können Module eventuell in dieser Art und Weise auf die API einfluss haben?

Auch mit dem ?useNumberAsId=true funktioniert der Aufruf bei mir. Vielleicht ein nicht-druckbares Zeichen zwischen articles/ und der ID? Ich frage weil ich genau Deinen Fehler hatte als ich aus Versehen das ? VOR die Id gepackt hatte. :slight_smile:

Und natürlich ist es theoretisch möglich dass Module hier in irgend einer Form eingreifen, testweise solltest Du diese auf jeden Fall einmal deaktivieren und erneut testen.

@hsoebbing schrieb:

Auch mit dem ?useNumberAsId=true funktioniert der Aufruf bei mir. Vielleicht ein nicht-druckbares Zeichen zwischen articles/ und der ID? Ich frage weil ich genau Deinen Fehler hatte als ich aus Versehen das ? VOR die Id gepackt hatte. :)

Und natürlich ist es theoretisch möglich dass Module hier in irgend einer Form eingreifen, testweise solltest Du diese auf jeden Fall einmal deaktivieren und erneut testen.

Nochmal danke für Dein Interesse, hatte nun noch einmal komplett ohne irgendwelche plugins getestet und kam auch nicht weiter, nun habe ich mal Nummer zu den maindetails und name zum Artikel hinzugefügt und es geht. Nur ist es leider so, dass im Shop eventuell der Name angepasst wurde, sodass ich nun immer den Namen erst vom Shop laden muss um den Artikel wieder hochzuladen. Das scheint mir alles nicht optimal, ich übersehe sicherlich irgend einen Haken oder eine Option.

Hallo BaynDev,

I agree with you that the REST API doc doesn’t really show an example if you want to update “mainDetail” of an article.

As this “mainDetail” has a primary key “id”, I think Shopware needs this info to know which mainDetail record it has to update. As I don’t see myself the reason why it is there (each article has only one mainDetail), I proceed as follows to update only stock data of articles:

  1. In my local list/table of articles, I have added a field “shop_id”, which is at the beginning NULL (= never uploaded to shop);
  2. At the first sync, as this field is NULL, I create the article in the shop (sending all info like name, stock, prices, etc). When the article is successfully created, the POST returns me the article id in the shop, which I save in my “shop_id” field in my local article table.
  3. When I later want to sync (when only stock has changed), I use this field to make my PUT like this (for example for article with id 2041):

{ "id": 2041, "mainDetail": { "id": 2041, "inStock": 5, "stockMin": 1 } }

So anything can be changed (name, translations, whatever) in the shop without overwriting it with the REST API, as the only thing that does not change is the “id” of the article, and only this information is used to access/identify the article to update. Saving the id when creating the article the first time also eliminates the need of a GET (to get any changes or other data to identify the article).

Hope this helps you on your way with your app !

@WillemMeert schrieb:

Hallo BaynDev,

I agree with you that the REST API doc doesn’t really show an example if you want to update „mainDetail“ of an article.

As this „mainDetail“ has a primary key „id“, I think Shopware needs this info to know which mainDetail record it has to update. As I don’t see myself the reason why it is there (each article has only one mainDetail), I proceed as follows to update only stock data of articles:

  1. In my local list/table of articles, I have added a field „shop_id“, which is at the beginning NULL (= never uploaded to shop);
  2. At the first sync, as this field is NULL, I create the article in the shop (sending all info like name, stock, prices, etc). When the article is successfully created, the POST returns me the article id in the shop, which I save in my „shop_id“ field in my local article table.
  3. When I later want to sync (when only stock has changed), I use this field to make my PUT like this (for example for article with id 2041):

{ "id": 2041, "mainDetail": { "id": 2041, "inStock": 5, "stockMin": 1 } }

So anything can be changed (name, translations, whatever) in the shop without overwriting it with the REST API, as the only thing that does not change is the „id“ of the article, and only this information is used to access/identify the article to update. Saving the id when creating the article the first time also eliminates the need of a GET (to get any changes or other data to identify the article).

Hope this helps you on your way with your app !

 

 

Thanks for all the responses and yours in particular, but for some reason, all of my attempts did not really help in any way shapre or form. I will now try my old code with a completely new installation of SW5 hoping to get SW5 to work with my code again. Again, thanks for all the effort. 

I wonder why no one is coming up with the following problem I have:

I use Shopware for internet sales and I’m also selling goods from the same stock in my store. My cashregister uses the Shopware REST interface to synchronize products and must also update the inStock value in Shopware when something has been sold in the shop.

The problem: There is no way to decrement the inStock value, I must get the current value and then send the decremented value (or incremented value in case something has been returned). The subsequent get and put actions are not transactional. So it can happen that the stock has changed in Shopware and the new stock value in Shopware gets updated to a wrong value.

Introducing a decrementStock property could solve the problem.

 

Looks like a good idea. Did you created already a issue? https://issues.shopware.com/createissue or even feel free to contribute  Halo