Saturday, December 20, 2014

Working with Commerce Connect - Getting Started

Standard

Introduction

We recently started work on a Sitecore 7.5 commerce solution for a large client of ours where we would implement the shiny, new Commerce Connect 7.5 (Connect) module. 

As a good architect / developer, I consulted with the powers of Google but couldn’t really find a whole lot of good information out there about how to get started. So, this blog series will report what I learned when implementing the module. In this article, I’ll explore the setup process and how to perform a simple test to make sure I can add and retrieve products from the product repository.

It’s not free

It is important to note that unlike its predecessors Sitecore E-Commerce Fundamentals Edition (SEFE) and Sitecore E-Commerce Services (SES), Connect is not free. I am not sure of the actual pricing; you would need to contact a Sitecore sales representative to get the details.

Installation

After downloading Connect 7.5, I went ahead and installed it on my local 7.5 development instance.

Next, I went ahead and set up my product repository and index. I followed Sitecore’s Installation
Guide to get my repository up and running: http://sdn.sitecore.net/upload/sdn5/products/sitecore%20commerce%20connect/7.5/sitecore_connect_75_installation_guide_usletter.pdf  
  

The 3 major steps include:

  1. Creating a repository using the Templates/Branches/CommerceConnect/Products/Product Repository branch. I set mine up at the following path:
    /sitecore/content/Data/Product Repository/Products

  2. Updating the search Source for the Manufacturer and ProductType fields of the Product template

  3. Modifying the necessary settings in the config files to let the module know where my new  repository is located in the content tree. Files and locations to update include:    
     
    Sitecore.Commerce.Products.config



    Sitecore.Commerce.Products.Lucene.Index.Master.config AND Sitecore.Commerce.Products.Lucene.Index.Web.configs


      With that behind me, I needed to go ahead and sync my product repository bucket before using it for the first time:



Pulling out a product

Ok, so according to the documentation, I should be ready to roll and can start throwing products into my repository where I can access them using the Sitecore search API.

In the final solution, I want to pull a product out of the repo by product name, because I intend to have a MVC route that will have the product name hanging off the end. Something like this: running/mens/running-shoes/support/wave-inspire-11

With that being said, let me write some code and give it a test.

POCO

The first thing that I need to do is create a POCO that is consumable by the LINQ layer within Sitecore. I have to make sure that I inherit from SearchResultItem:

 public class ProductSearchResultItem : SearchResultItem  
   {  
     public string BrandName { get; set; }  
     public string ExternalID { get; set; }  
     public string Divisions { get; set; }  
     public string FullDescription { get; set; }  
     public string Identification { get; set; }  
     public string Manufacturer { get; set; }  
     public string ModelName { get; set; }  
     public string Product_Name { get; set; }  
     public string ProductClasses { get; set; }  
     public string ProductType { get; set; }  
     public string Short_Description { get; set; }  
   }  

GetProduct Method

Next, I want a method that will take a product name as a parameter, and return the actual Sitecore item from my repository. In my case, I am using Glass Mapper as my ORM of choice. If you are still writing your classes by hand, or are using Custom Item Generator to generate your template based classes, I highly recommend that you look into using Glass.


 public Product GetProduct(string productName)  
     {  
       var index = Settings.Indexes.GetIndex("products");  
       using (var context = index.CreateSearchContext())  
       {  
         IQueryable<ProductSearchResultItem> query = context.GetQueryable<ProductSearchResultItem>()  
           .Where(  
             resultItem =>  
               resultItem.TemplateId == IProductConstants.TemplateId &&  
               resultItem.Product_Name.Equals(productName, StringComparison.InvariantCultureIgnoreCase) &&  
               resultItem.Name != "__Standard Values");  
         SearchResults<ProductSearchResultItem> results = query.GetResults();  
         var queryResults = results.Hits.Select(hit => hit.Document.GetItem());  
         return queryResults.Any() ? queryResults.FirstOrDefault().GlassCast<Product>() : null;  
       }  
     }  


This is what my code looks like that will actually implement the method (some ugly hard-coded values, but remember that this is simply a test):

 var myProduct = new ProductManager().GetProduct("Test Product");  

Creating a Test Product

For the final piece of the puzzle, I need to add a test product to my repository.


Run it!

In my test, I slapped together a quick controller rendering as we are using MVC for the project. If you are using Web Forms, you would simply throw this onto a Layout or Sublayout.

The breakpoint below the line where I called my method confirmed that I am cooking with grease!


In Summary

I was successfully able to:

  1. Install and configure Commerce Connect.
  2. Add a product to the product repository.
  3. Write some code to pull the product out of the repository by product name using the Sitecore.ContentSearch API.
In my next post, I will be showing how to work the Commerce Connect cart, so watch this space.

Happy Coding!

1 comment:

  1. Where does the ProductManager() class come from?
    -Denis

    ReplyDelete