As Joshua points out, cookies are not the best place to store cart information. That type of information should be kept on the server. Depending on your requirements, that could be session data or a database. Storing cart information on the client does not allow any insight into cart contents. For example, if someone leaves items in the cart, you can engage them by sending reminders or adding messages to the storefront page.
To answer your question, cookies are strings, so if you want to store your cart data structure as a cookie, you need to serialize it. Refer to this earlier question for a discussion on the technical merits of serialize
and json_encode
.
The term "web storage" is a bit ambiguous. Are you referring to the HTML5 local storage? If so, that's probably not a good option since the data is not automatically sent to the server on each request as is done with cookies.