How to get existing cart token (for anonymous users)

Created on 5 November 2024, 5 months ago

Problem/Motivation

https://www.drupal.org/docs/8/modules/commerce-api/cart-and-checkout/car... perfectly describes how to create cart tokens, but when combining headless with a regular Drupal Commerce Shop (e.g. implementing a configurator for specific items, which uses commerce_api, but using the regular Drupal Commerce UI for regular products)?

So the question is: How to get the cart token of an eventually already existing cart from the user's current browser session (as anonymous user)?

The heading could be:

Get session cart token

This may be required, if a cart is created by adding an product to cart using the regular shop IU, before commerce_api is used in the configurator. Then the existing cart needs to be reused, to not create two carts.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Active

Version

1.0

Component

Documentation

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Anybody
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇩🇪Germany Anybody Porta Westfalica
  • 🇮🇱Israel jsacksick

    If the API calls are performed from within the same domains, then cookies are passed alongsides requests.
    Otherwise, in case it is running from a different domain, the JS application as explained in the API docs you've linked is responsible for creating the cart token and passing it as a header for each API request made.

  • 🇩🇪Germany Anybody Porta Westfalica

    Thanks for the quick reply @jsacksick!

    Still the docs and your answer don't explain how to get the existing cartToken to pass, when adding another item to an existing ("regular") cart using commerce_api. Or if, in such a case, no cartToken should be passed at all?

    (Hope it's fine to reopen for that reason)

  • 🇮🇱Israel jsacksick

    What you're describing isn't possible.
    Either you do Headless and you generate the cart token and pass it for all requests, or for a mixed scenario like the one you're describing, the regular HTTP headers should suffice... As the mechanism for getting a cart ID from the session should kick in, assuming the requests are performed.

    In a scenario where the API request is performed from the same domain, the session cookie should be passed, and the cart ID should be fetched from the session itself.

  • 🇩🇪Germany Anybody Porta Westfalica

    In a scenario where the API request is performed from the same domain, the session cookie should be passed, and the cart ID should be fetched from the session itself.

    Yeah that's what I was thinking and talking about. It's the same domain, as it's the same shop. We just should not generate the cartToken.

    Thanks for the clarification that this is node intended to be mixed!

    We'll add more details here, once we have a final implementation. I think it might help others.

  • 🇩🇪Germany Anybody Porta Westfalica

    Not setting a cartToken at all works for this case! @Grevil will provide details tomorrow.

  • 🇩🇪Germany Grevil

    Not really many details to share here! We simply removed the Token-Generation described in https://www.drupal.org/docs/8/modules/commerce-api/cart-and-checkout/car... from our code, as our application resides in the same domain.

    I just find the documentation a bit misleading:

    Cart tokens are used to authorize cart and checkout requests for anonymous users. Without specifying a cart token, cart operations will fail and not act upon any existing carts in the system that the user may have created.

    Nowhere it is stated, that this is only the case for headless applications. I'll adjust the documentation accordingly!.

  • 🇩🇪Germany Anybody Porta Westfalica

    Great, thank you all!

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024