how to manage shopping cart sessions without login

In MVC - In E commerce - how to add products in add to cart without Login and managed this session after Login and also show those products which are added before Login Like Flipkart and Amazon.

its very urgent for me because my site is delay for this.



Hemant Modi 

: 847 At:- 6/7/2017 4:57:23 PM mvc sql javascript json

Hemant , can you use Sessions for this? Or it is not allowed? 0
By : vikas_jk - at :- 6/7/2017 5:07:42 PM

1 Answers
profileImage Answered by:- vikas_jk

Hemant, I have solution for you, maybe it is correct for your requirement

Step 1: If someone adds a product in shopping cart, save a cookie in user browser, you can use javascript/jquery to save cookie in users browser

function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    document.cookie = name + "=" + value + expires + "; path=/";

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    return null;

function eraseCookie(name) {


//Create Cookie for 1 year, when user add's a product

//Save Cookie Unique_Guid in database, with product using ajax call

var x = readCookie('ppkcookie')
if (x) {
   // do something here, like load shopping cart products from database using ajax


Step 2: Save products in database with unique_id(if user add a product in cart), and fetch products from database where unique_id matches

Let me know if this work's for you, thanks

At:- 6/7/2017 5:53:41 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use