shopwareshopware6shopware6-api

Retrieve shopping carts from user sessions


I want to retrieve customer shopping carts, I call the /checkout/cart route via the Store API. I pass in the SalesChannel access token as well as the sw-context-token of the store visitors.

For some sw-context-tokens, a new cart with a new token is returned to me, even though the token exists in the database.

Also some tokens have the suffix _backup. These can be retrieved, but the token without suffix not, although the shopping cart is more current.

I would need hints and explanation why the system behaves like this.

My requests look like this

$client = new Client();
$response = $client->request('GET', $shopware->url . '/store-api/checkout/cart', [
    'headers' => [
        'Accept' => 'application/json',
        'sw-access-key' => 'SWSSXXXXXXXXXXXXXX',
        'sw-context-token' => $contextKey
    ],
]);

return json_decode($response->getBody()->getContents(), true);

Solution

  • By providing the token via the sw-context-token header, the system will try to resolve the previous context stored in the table sales_channel_api_context. By the time you make the request, the token may simply have expired. Thus the old token will be invalidated and a new token will be assigned. The record in the cart table may then be considered abandoned and should get cleaned up by an automatic task at some point.

    Check that the token still exists in the sales_channel_api_context table and isn't expired. By default, the record is considered expired when the updated_at date is more than one day ago. (can be changed with shopware.api.store.context_lifetime)

    Furthermore, you should still be able to retrieve the abandoned carts by providing the token explicitly as a query parameter:

    GET /store-api/checkout/cart?token={token}
    

    As for the _backup suffix, I couldn't find any occurrences of a string like that at all in shopware/core. Could it be a third-party plugin?