A shopping cart temporarily stores selected items before purchase.
- Add items to Cart.
- If the user is not logged in, save items to the browser cache.
- If the user logs in, merge cached items with server-side user data.
- If the user logs out, sync cart items across all platforms (browser, mobile, etc.).
- Display cart items
- Checkout selected items.
Cookie or LocalStorage Storage:
- Cookie: for small data (< 4KB).
- LocalStorage: for larger data.
Data Structure:
- Cart (list)
- Item
- sku_id (product ID)
- quantity
- timestamp (added time)
- selected (true/false)
- Item
Example:
{
"cart":[
{
"sku_id": 123,
"quantity": 2,
"selected": true,
"timestamp": 1578772233
}
]
}Store persistent cart data in a database table.
Data Structure:
- Cart (table)
- id
- user_id
- sku_id
- quantity
- selected
- timestamp
Use Redis to store temporary cart data per user.
Data Structure:
- KEY (user id)
- VALUE (list)
- Item
- skuId (product ID)
- quantity
- timestamp
- selected
- Item
Example:
{
"KEY": 567,
"VALUE":[
{
"skuId": 123,
"quantity": 2,
"selected": true,
"timestamp": 1578772233
}
]
}