Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

haniabidkz's avatar

Adding guest data into cart [JWT REST API]

I am developing an eCommerce application rest API. This API will be consumed by mobile developers to build an e-commerce app.

I am facing an issue regarding the shopping cart, A user without being logged-in can add the data to the cart and the data will be stored in a database table named cart. Cart table has product, quantity, guest_id (For guest id), user_id(For authenticated user id) columns.

If logged-in user adds a product to the cart, his ID with cart data will be inserted in the table. But what about guest_id, How I can generate guest id, and where I can store it so it will remain the same and I will be able to access the guest cart data on the cart page?

Note: I am using JWT for authorization/authentication purpose and using core php.

0 likes
3 replies
martinbean's avatar

@haniabidkz If it’s a RESTful API, then you shouldn’t be relying on state. So, you should be creating a cart resource, and then using the identifier from that cart resource to add items to the cart. You can then associate the cart with a user if the user authenticates at a later time.

haniabidkz's avatar

But my question is exactly this: How to associate the guest user to the cart so later on, he can be turned into an authenticated customer?

martinbean's avatar

Because you use tokens to authenticate a user. If no token is provided when creating a cart, then they‘re a “guest”. If a token is provided when creating a cart, create the cart and associate it with the user linked to the token.

Please or to participate in this conversation.