# Thursday, 15 March 2012

imageA great tool for converting a customer is to display items they’ve previously looked at. One way to do it is by using persistent baskets, i.e. the basket contents remain the same even if a customer leaves and later returns to the store.

But what about the cases where the customer didn’t actually put anything in the basket, but we still want to be able to show some of the items she browsed recently?

Approaches

The most obvious approach is to store SKUs in a cookie and present that list to the customer, but it makes for some pretty trivial use cases down the road, so let’s explore a different way of doing it: A custom basket.

uCommerce comes with the ability to handle as many baskets per customer as you like so why not leverage this to create almost a wish list-like scenario. After all the recently browsed list is just a list of products that sure sounds like a basket?

Creating a New Basket

First up we need to create a new basket. By default uCommerce will do one default basket where every item goes into, but we’ll need a second basket, which we can maintain ourselves.

Let’s do a little Razor, which will create that custom basket and tie it to the customer using a cookie:

   1: @using UCommerce.Runtime;
   2: @using UCommerce.EntitiesV2;
   3: @using System.Web;
   4:  
   5: @{
   6:   ProductCatalogGroup catalogGroup = SiteContext.Current.CatalogContext.CurrentCatalogSet;
   7:   ProductCatalog catalog = ProductCatalog.All().Single(x => x.Name == SiteContext.Current.CatalogContext.CurrentCatalogName && x.ProductCatalogGroup == catalogGroup);
   8:   
   9:   Guid basketId = Request.Cookies["viewedProductsList"] != null ? new Guid(Request.Cookies["viewedProductsList"].Value) : Guid.Empty;
  10:  
  11:   PurchaseOrder viewedProductsList = basketId != Guid.Empty 
  12:                                         ? PurchaseOrder.All().SingleOrDefault(x => basketId != Guid.Empty && x.BasketId == basketId)
  13:                                         : new PurchaseOrder();
  14:  
  15:   // Initialize new order
  16:   if (basketId == Guid.Empty)
  17:   {
  18:     viewedProductsList.OrderStatus = OrderStatus.Get((int)PurchaseOrder.StatusCode.Basket);
  19:     viewedProductsList.ProductCatalogGroup = catalogGroup;
  20:     viewedProductsList.BillingCurrency = catalog.PriceGroup.Currency;
  21:     viewedProductsList.BasketId = Guid.NewGuid();
  22:     viewedProductsList.CreatedDate = DateTime.Now;
  23:  
  24:     HttpCookie cookie = new HttpCookie("viewedProductsList", viewedProductsList.BasketId.ToString());
  25:     cookie.Expires = DateTime.Now.AddDays(30);
  26:     Response.Cookies.Add(cookie);
  27:   }
  28: }

Adding a Product To The List

Once we have the custom basket set up we’ll just start adding items to it in the usual manner from the product page or whenever you deem that the customer showed enough interested to have a product on the recently browsed list.

   1: // Add product being displayed
   2: Product product = Product.All().Single(x => x.Sku == "100-000-001" && x.VariantSku == "003");
   3:  
   4: viewedProductsList.AddProduct(catalog, product, 1);
   5:  
   6: viewedProductsList.Save();

Displaying The Recently Browsed Items List

The third and final piece is to display the list. The code will basically have to look up the cookie tying the list to the customer, load the basket with contents, and display individual order lines to the customer. Simple stuff and here’s how to do it:

   1: @using UCommerce.EntitiesV2;
   2: @using System.Web;
   3:  
   4: @{
   5:   // Find the cookie containing the basketId
   6:   Guid basketId = Request.Cookies["viewedProductsList"] == null 
   7:                     ? Guid.Empty
   8:                     : new Guid(HttpContext.Current.Request.Cookies["viewedProductsList"].Value);
   9:  
  10:   // Load the list
  11:   PurchaseOrder viewedProductsList = basketId != Guid.Empty 
  12:                                 ? PurchaseOrder.SingleOrDefault(x => x.BasketId == basketId) 
  13:                                 : new PurchaseOrder();
  14: }
  15:  
  16: @foreach (var orderLine in viewedProductsList.OrderLines.OrderBy(x => x.Quantity))
  17: {
  18:     <h3>@orderLine.ProductName</h3>
  19:     <p>Bought @orderLine.Quantity times.</p>
  20: }

In Summary

We took a look at what it takes to maintain a separate custom basket for handling a recently browsed items type list. Using a custom basket instead of storing SKUs in a cookie has the added advantage of the extra information available when displaying the list to a customer.

Best of all it takes a trivial amount of code to get it up and running.

The code above sorts the list by quantity; the premise being that the product will have its quantity increased every time the customer looks at it thus we have a handy mechanism for determining the most popular items on the list. You can expand this to look at last modified dates on order lines to see when the customer was last interested in the product and so forth.

Finally the technique outline here is handy for scenarios like gift registries, wish lists, standard recurring orders, and other cases which require a custom list of products maintained either by the customer herself or by the store.

Comments are closed.