Recover shopping cart after closing and opening browser

1

I'm building a shopping cart with JSF it's working, but not the way I think it's the right one.

Example: When I enter sales sites, even though I'm not logged in, I can add items to the cart, and even closing and opening the browser will keep those items there. In my case, when I exit the browser and enter again the items do not remain, because the session changes when the browser is closed, in this case, it is useless to save the session in items to retrieve it when the user returns to the site. I already researched the use of cookies for this case, but it did not help me at all. Could someone help me by telling me how I could implement this situation? If it is implemented using cookies and same session?

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

    
asked by anonymous 13.03.2018 / 17:56

1 answer

1

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

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

To identify the client only, a good option would be to use a cookie. The cookie is persisted in the client's own browser, usually in a text file, for 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 to a database and assign a unique ID to that set of saved items. You could take advantage of JSESSIONID itself if you do not want to generate a random String. Remember to update the database each time the user modifies something in the cart by placing 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);

At each site visit, for which there is no active cart yet you search for the cookie, get the ID, and search for the corresponding items in the database. If they exist, you put them in the cart that is in the session. It's important to check first so you do not run the risk of overwriting an upgraded 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 make the database purge the abandoned records. To do this, save the date in each cart record.

    
16.03.2018 / 14:32