2

I want to give an ID to a user that opens my website and adds some items to their "shopping cart", even if they aren't signed up to the service. Just add, then proceed to sign up when they want to check out. And if they don't go to checkout after adding stuff, then close their browser, come back 2 days later, I want to retrieve their previous orders from database.

How do I give this ID that is unique to a user and remember it next time they visit?

I assume I need to use cookies, but don't know how exactly?

Tolga Açış
  • 168
  • 1
  • 16
  • 1
    you can use cookies or html5 localstorage – Niladri Dec 11 '17 at 12:24
  • Like I said, I assume that I need to use cookies, but how do I use them and how do I use them with asp.net? – Tolga Açış Dec 11 '17 at 12:27
  • are you using webforms? – Niladri Dec 11 '17 at 12:27
  • 2
    You could try searching google for something like "asp.net using cookies" - [top result](https://msdn.microsoft.com/en-us/library/ms178194.aspx). You will not usually get a very good response on SO if you don't show that you have made some effort of your own. Typically you would even show the code you have tried so far & explain why it doesn't appear to be working. You will get much more help that way. – PaulF Dec 11 '17 at 12:31
  • Using MVC as my tags say – Tolga Açış Dec 11 '17 at 12:32
  • 1
    Consider generating a GUID, and store it in a cookie. – mjwills Dec 11 '17 at 12:34
  • I don't know anything about cookies and only recently started learning web development. So I can't really show anything I made because I don't know how to make it. I can make a function that saves a new 'temporary' entry to database when a user opens the website, then returning the ID from that new entry from database, but that may not be the right way since multiple users open a website so starting a new entry THEN returning ID (since ID is assigned by SQL) may be the wrong thing to do, since latest ID may belong to another user. – Tolga Açış Dec 11 '17 at 12:49

2 Answers2

1

I have done something similar using cookies and a database. So in c# you could have a Basket table, and in that table have a UserId column along with a ProductId column. Then from your controller you would pull the users basket where the UserId is of that in the database.

Setting the cookie:

string cookieValue = Guid.NewGuid().ToString();
//Creating a cookie which has the name "UserId"
HttpCookie userIdCookie = new HttpCookie("userId");
userIdCookie.Value = cookieValue;
//This is where you would state how long you would want the cookie on the client. In your instance 2 days later.
userIdCookie.Expires = DateTime.Now.AddDays(3);
Response.SetCookie(userIdCookie);

Then to get the cookie in the controller:

public ActionResult Basket()
{
    //Getting the cookie which has the name "userId" and assigning that to a variable.
    string userId =  Request.Cookies.Get("userId").Value;
    var basket = _context.Basket.Where(x => x.UserId == userId);       
    return View(basket);
}

Note: I have used Request.Cookies.Get("userId") here because if you use Response.Cookies.Get("userId"), and the cookie "UserId" does not exist, then it will create the cookie for you.

Daniaal
  • 882
  • 8
  • 16
  • I already have an "orders" table, which is connected to a "customers" table, I just want to make a temporary customer, and save this temporary customer's data on a cookie for that customer, that is all. – Tolga Açış Dec 11 '17 at 12:51
  • Trying this now, figured out how to do it. – Tolga Açış Dec 11 '17 at 13:22
  • It worked perfect after some little tweaks, I added a small condition to get cookie first, and if not any is get, only then assigning a cookie (otherwise it created a new user every time Homepage was opened). Thank you! – Tolga Açış Dec 11 '17 at 13:55
  • ah yes sorry. You could just do if(Request.cookies.Get("userId") == null) {Response.SetCookie("myCookie");}. You could set this in the Products controller get method so when a user hits that page the userId would be assigned. And glad i could help :) – Daniaal Dec 11 '17 at 15:29
0

When the user adds something to cart, run javascript like so:

 var storedId = localStorage.getItem('myId');

if(storedId == null)
{
   storedId = parseInt(Math.Random * 1000); // or better, use UUID generation from here: https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript

   localStorage.setItem('myId', storedId); // for future reference
}

Now, whenever you add something to cart, post the id, e.g.

Controller:

[HttpPost]
public ActionResult AddToCard(string userId, string productId, int quantity)
{
  /// perform your saving to db
}

Ajax (or whatever framework you use):

$.post('/somewhere', {userId: storedId, productId: 'whatever', quantity: 1});
zaitsman
  • 8,984
  • 6
  • 47
  • 79