Add existing product to order with shopware 6 api

30 views Asked by At

I am trying to edit an order that have been giving up.

This order may has a product that I want to swap with an other product. Since there are some unique identifier, its kind of complicated now to tell what item you want to delete. How ever, I am currently getting the lineItems from the order, searching the payloads for the product number and and if I found it, I take the unique id and delete the product.

So fine so fare...
Now I want to add a new product (that actually exists in the SWDB). Goal is, to pass only the required informations to the API to add that product to the order.

For some reason I can't find anything about this in the internet or I am very bad at searching. How ever, my tests failed all and currently I was'nt able to deal with this (what I actually consider as common needed action) problem, adding a product.

As I sayed, the documentation does'nt seem to provide me with enough information to do so. Am I the only one who want to add a product via api? I strongly guess not :D

So does anyone can hint me in the right direction?

1

There are 1 answers

0
Dwza On

I finaly made it and add my experience here to help others may having the same issue.

Steps in order to go:

  • Get Order (to have order data)
  • Create a version of the order
  • Do your edits
  • Merge version into order

Description

To add a product to an existing order basically have to call the endpoint

_action/order/{unique_order_id}/product/{unique_product_id}

As body you have to pass at least the quantity

{ "quantity": 10 }

CAUTION: In order to do this, you HAVE to create a version of the order.

You can do this with

_action/version/order/{unique_order_id}

This will return an array with a version number versionId and some other fields.

You have to add this versionId to your header

Sw-Version-Id: {versionId}

before sending any updates to the sw order.

Now you can do your edit.

When you are done with them edits, you have to merge the order version into your actuall order. You can do this by running this endpoint

_action/version/merge/order/{versionId}

After merge, shopware calls a recalculation to have the price calculated correctly.

I hope this helped.

Helpfull information
If you look into your browsers console, you actually can see every api call in the network tab. So you can perform an action in the admin section and look what happend in the network tab to build your stuff.