What is a Cart Key?
The cart key is a unique identifier for the cart session and is used to retrieve the cart data from the database when making requests to the Cart API.It is important to note that the cart key is not the same as the user ID, but it can be related to the user ID if the user is authenticated.
Finding the Cart Key
You can find the cart key in two places. Either returned in the cart response calledcart_key or via the returned headers called Cart-Key.
Using the Cart Key
Once you have the cart key, you can then use it to set thecart_key as a global parameter with any of the Cart API routes to load that cart or pass it as a header.
Without the cart key set for other Cart API routes, any requests made will not be applied to the appropriate cart session. This is partially important when adding, updating or removing items to and from the cart as such. The same for coupons, fees and other cart related requests made.
Creating a Cart Key Manually
You can create your own cart key but, it cannot be longer than 42 characters as that is the limit for storing the key in the database.HTTP request
/wp-json/cocart/v2/cart?cart_key=mycartkey123
If you do create your own cart key, it is best that you do so when adding the first item to the cart in order to update the correct cart from the start.
Troubleshoot
Why am I NOT seeing the 'Cart-Key' header returning?
Why am I NOT seeing the 'Cart-Key' header returning?
If the header is not returning then you will need to enable CORS so that the header is exposed.