Ideal way to store product information in shopping cart in asp.net
You shouldn't implement like this. Sessions of 10 hours are very long, if you have 1000 users on your website your server is going to suffer and will create some performance issues.
On our ecommerce websites we create a cookie for anonymous users. After doing this you have two solutions:
- Store the entire basket in the cookie (basically list of product ID + quantity, more depending on your needs/strategy) or other techniques (e.g. localStorage).
- Store a unique ID in the cookie and store the basket in the database (temporary table) with the unique cookie ID as identifier. Once the user log into the system you move those data to the user basket table.
You can easily change the expiration time of cookies (or remove it) without impacting the overall performance of your server. For IDs you can use Guid.NewGuid()
.
UPDATE:
More details about my second solution - scenario:
- User visit website
- You create a cookie with no expiration date: cartId with value
Guid.NewGuid()
(name and value are up to you). - User click on "Buy"
- Get the cookie server-side and add the cartId,product/quantity to the "AnonymousBasket" table
- Imagine the user leaves the website and come back a month later the cookie will be still here
- If the anonymous user go to his basket you are able to get his products and quantities based on the value of the cookie.
- If the user log on you remove the cookie and move data from the table "AnonymousBasket" to "Basket"
- Now your logged in user can process to the checkout
With this solution everything is persisted in the database and doesn't require sessions. You can change the expiration of your cookies like you do for sessions, alternatively you can cleanup the temporary table without any risk or create statistics on anonymous users (why they didn't proceed to checkout, how many items in average, which item, ...).