william007
william007

Reputation: 18535

Adding items to a shopping cart by users before registration/authentication

may I know how should I allowed not-login user to add items? (We are using customized solution without using an open source eCommerce package)

The consideration is:

  1. Store at database: If the not-login user information is stored in database as guest user(create a newid for each user and store that id as in session of the browser), then how should we know when to delete them?

  2. Store at client: If all information is maintained in cookie, it would need to write a seperate set of code for this, that would be troublesome.

Is there any suggestion?

Upvotes: 4

Views: 8023

Answers (2)

nickhar
nickhar

Reputation: 20833

You can do either - both techniques are valid, BUT in many cases, e-commerce sites use a cookie to store the contents of a temporary shopping-cart if the user hasn't yet been authenticated by logging-in or by registering.

NOTE: Even if you use the DB to store the temporary cart, you still need to set a cookie to identify them in a rudimentary way:

  // Temp user id
  $cart_id = "108376"; // Pulled from DB

  // Two week cookie (86,400 secs per day for 14 days)
  setcookie('abc_cart_id', '$cart_id', time() + 86400 * 14);

-- OR --

Using cookies, once they login or register, you could write the contents of the cookie (and cart contents) to your DB. That way, you're not adding unnecessary entries to your database that are orphaned or that need to be discarded at a later date (and a process to manage it).

In many commercial solutions I've seen (using PHP and sometimes containing hundreds of items), the cookies will often take the form of a serialized array or using JSON:

  // Serialise array
  $cart = serialize($cart_contents);

  // Two week cookie (86,400 secs per day for 14 days)
  setcookie('abc_cart', '$cart', time() + 86400 * 14);

Which is then used to re-populate their cart when the new user returns:

  if (isset($_COOKIE['abc_cart']))
  {
     $cart = unserialize($_COOKIE['abc_cart'];
     // Take action ~ populate cart/DB etc...
  }

It's also useful in CRM terms, where you can change the UX/make them offers or incentives based on the fact that they haven't signed up/aren't a customer yet ~ 20% off your first order...

In both Cookie and DB approaches, you need to consider the lifetime of the cookie set as the user in question may be on a shared computer and set it based upon what you consider appropriate.

Upvotes: 2

Terradon
Terradon

Reputation: 858

Set a unique cookie, store this in your database, including added items. So connecting basket en unique visitor together. This way, when your potential buyercomes back, you still know his basket/interests Dont bother about deleting, unless you have billions of records in your database. However, storing for a few weeks should be enough:)

Upvotes: 1

Related Questions