Recover shopping cart after close and open browser

Asked

Viewed 339 times

1

I’m assembling a shopping cart with JSF it’s working, but not in the way I believe is correct.

Example: When I enter sales sites, even if I am not logged in, I can add items to the cart, and even when I close and open the browser these items remain there. In my case, when I leave the browser and enter again the items do not remain, because the session changes when the browser is closed, in which case it is useless to record the session in items to recover it when the user returns to the site. I’ve researched the use of cookies for this case, but nothing helped me. Could anyone help me by telling me how could I implement this situation? If it is implemented using cookies and same session?

I am using JSF, JPA. Any help I will be very grateful!

  • Yes, saved in the cookie some information for you to retrieve the shopping cart.. of course you should save it on the server side as well

  • I tried to make an example using JSESSIONID, if I do not leave the browser it works well, look at the output: Session Id = D5913F3F699B66B8AFEDD32E108F9C92 //new JSESSIONID cookie = F688E7A616018963013062251D16A805 //previous session, but when I close the browser and open again it ignores the previous session look what comes out: Session Id = 944D48C7F4BE1F807B207CC9694A629E JSESSIONID cookie = 944D48C7F4BE1F807B207C9694A629E In this case it is a test I am doing, it will be if the problem is because in this case, I haven’t saved that session to the server yet?

  • You will not go back to previous session, closed the browser it got lost, you will start a new session. Your cookie may have some information that identifies the user, a token, id, etc., so you start a new session... of course the cart needs to be saved on the server side in order to recover. You can use a bank nosql as Mongodb to save the cart, and when effective save to your transactional bank

  • When the user is logged in I do not have this problem, because save the item with the user’s identification, the problem is when the user is not logged in. It would be correct to make this identification by ip?

  • In this case you could generate a temporary identifier while not logged in. IP is not a good idea because if it is behind a proxy, you may end up reading only the IP of the company proxy for example, and if more of a user of the same company access the site, will have the same IP, which will be a problem (everyone will see the same cart)

  • I thought the same thing, really ip is not a good idea

Show 1 more comment

1 answer

1


The session can only store attributes while the client is interacting with your application within the validity period of that session. In case of inactivity for a longer period than the validity or if the user closes the browser, the session is lost. You have already verified this by checking 2 different Jsessionids when closing and reopening the browser.

Therefore, if you only use the session to maintain the status, you will not be able to solve your problem.

To identify the customer only, a good option would be to use a cookie itself. The cookie is persisted in the customer’s own browser, usually in a text file, during the validity you specify.

In this case, you would need to set a specific cookie for your purpose. Your code would have to save the cart items in a database and assign a unique ID to that set of saved items. You could take advantage of JSESSIONID itself if you don’t want to generate a random string. Remember to update the database each time the user modifies something in the cart, putting or removing some product.

The cookie would be created and sent to the browser, having as content that unique ID. Adjust the validity of the cookie according to your business requirement. It would look like this:

String valorCookieCarrinho = ID; //O que você quiser definir, basta ser único
Cookie cookieCarrinho = new Cookie("cookieCarrinho_br.com.seusite", valorCookieCarrinho);
cookieCarrinho.setMaxAge(60*60*24*7); //Defina a validade - 1 semana?
response.addCookie(cookieCarrinho);

On every visit to the site, for which there is not yet an active cart you search for the cookie, get the ID and search for the corresponding items in the database. If they exist, you place them in the cart that is in the session. It is important to check first, not to risk overwriting an updated cart.

Cookie[] cookies = request.getCookies();

if (cookies != null) {
 for (Cookie cookie : cookies) {
   if (cookie.getName().equals("cookieCarrinho_br.com.seusite")) {
     //pegar o valor do ID com cookie.getValue() e buscar no bd
    }
  }
}

Do not forget to have the database purge the abandoned records. For this, save the date on each cart record.

  • Hello William, thank you very much for the answer and for your examples. So my problem is that even creating a cookie with an identification I have no way of knowing if a user not logged in is the same who entered the system added items in the cart and for some reason, lost the session returned to the site and lost everything that had already entered. I did it in a way that I do not know if it is correct, but it was the way I found, I am identifying the user not logged in by the "external" ip and I am saving this information as id, the moment any person enters the cart I search for data by IP.

  • That way, I did some tests and this responding well, a problem is that the IP suffers changes from time to time, from yesterday to today mine has already changed twice, but because the items stay in the cart for a certain time, the risk is not so bad.

  • By IP is really not the best way. Mainly because several clients may have the same IP. In the case of a company’s network, for example, there is usually a proxy that is in the output to the internet with the so-called "edge IP".

  • The cookie should work, using a single ID, as I explained. Edit the answer and describe what you did better. At what point you are setando cookie? At what point are you recovering your value? Remember that the session will never be the same. No use trying to fetch data in the new session.

  • 1

    Hello William, I studied the situation a little more and implemented with cookie, I followed your idea but implemented using the library of Ominifaces. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.