Lukas Niestrat
Lukas Niestrat

Reputation: 755

Shopware6 Cart API

I am building an Angular integration in Shopware 6, the whole store relies on the storefront theme, but the JS application can add products via the store-api as well. Now how can I add the item to the storefront cart? I post the data to the /store-api/checkout/cart/line-item API and even with context-token which I fetched from the /store-api/context API before, it doesn’t work. Is there any way I can add items to the storefront cart with the current session via the API? Or maybe I can get the current context output somewhere to then take the token and update the current cart?

Upvotes: 2

Views: 1210

Answers (1)

dneustadt
dneustadt

Reputation: 13161

You have the basic steps right, so it's not really easy to answer without full context.

  1. Retrieve the context token from the token property of the response body after a request to GET /store-api/context
  2. Request POST /store-api/checkout/cart/line-item with the context token set as the header sw-context-token and a valid payload as the request body. A minimal payload would look like this:
{
  "items": [
    {
      // the id of the product entity
      "referencedId": "801419a0c21645a8a71f70049422a056",
      "type": "product"
    }
  ]
}
  1. If all went well it should return the data for the updated cart in the response body
  2. You can verify that the cart persisted by requesting GET /store-api/checkout/cart with the same sw-context-token

I just went through these steps and that works. Keep in mind that the product must also be available for the sales channel you get the context token for, which in turn is determined by the sw-access-key header you use for authorization across all these requests.

Upvotes: 2

Related Questions