@glautrou's answer is pretty adequate, and I don't support doing this by session
either.
In addition to @glautrou's answer, I will explain how you can work on a single table without creating a different one.
Process steps:
- User visits your site
- Adds the product to the cart without logging into the site
- When adding the product to the cart, you create a
cart cookie
for the user. You may not have a time limit for this cookie or you may set a 1-3-6 month limit. (note that if the user deletes their cookies, there's not much point in keeping the cookies that long.)
- With this
cart cookie
, you save the product information in the Shopping Cart table (via ProductId
). (ProductId, UserCookieCartCode, Quantity etc.)
- As long as the user does not delete the cookie or the cookie expires, the user will see the products added to the basket before each visit to the site.*
*Of course, these products may be out of sale or the price may change. You can also do such checks.
Now let me explain them with some practice:
If the visitor is adding items to the cart without being a member, you create a cart cookie
and add this value to the Shopping Cart
table, for example in the VisitorCookieId
field.
If the visitor decides to log in, you match the logged in userID
with the VisitorCookieID
and update the UserID
field. The same will apply if one wishes to register a new one. This is an important criterion in terms of user experience.
Thus, you can manage the information of your logged in/non-member visitors from the same table.
An example of the Database Structure is as follows (exemplified for SQL Server
):
Here you can only create a ShoppingCart
table and keep information such as UserId
, VisitorCookieId
, ProductId
, Quantity
in a single table. However, this is not the right approach "in my opinion" because if the user converts this cart to an order, you have to take extra action as there will be more than one order line for the same user. In this structure, you trade on a single order information.
Maybe you can add an OrderId
field in the ShoppingCart
table and bind them when the cart is ordered. Of course, this will be valid unless you permanently delete the cart information that turns into an order.
Here are some important things; As I mentioned above, the user may delete the cookie, may not come to the site again, or may not purchase the products he has added to his cart. This is very important data for the user experience report for you.
E.g;
- If the visitor has added the product to the cart without logging in, the cookie ID is added to the VisitorCookieId field. Here you can analyze non-registered/not logged in visitors.
- If the visitor adds a product to the cart and then logs in, you will also update the UserId field that matches this cookie ID, as I explained in the top line. Thus, you can analyze the members who trade without logging in. The situation is the same for new registrations, in this case, you can look at the date of adding to the cart and the date of membership.
- You can examine those whose
VisitorCookieId
field is empty and UserId
field is full, as those who log in and take action. Of course, there is another analysis opportunity here. Do these members have any other orders with the VisitorCookieId field filled?
These examples can be multiplied. In summary, using Cookie instead of Session is beneficial in terms of db and application performance. Also, trading with Session will not always provide the same efficiency, even if you set the duration of your data, it may be lost before that time.
There may also be problems with cookies; The user can delete cookies or not allow cookies. In this case, we will respect the user's choice and make it clear to they that it will not get good performance from the site if they does not use cookies.
As with general standards, it is important to decide which approach is best suited for your structure. Instead of general standards, the solution you find suitable for yourself may be better. Here I have made a suggestion combining the general standards with the solution that suits me, I hope it will be useful for newbies to this page.
** If you want to periodically delete this data, move it to a different table or report it, you create a scheduled task structure for them. Quartz.Net
or Hangfire
will be useful for these.