Tuesday, October 26, 2010

Get product by category using commerce server

                         Recently we got a problem with in our application that is some product's bread crumb not displaying properly .I have investigated and find the problem.
Problem:       The problem is when product is associated with two categories it is always fetching from primary parent  category .
       
Example :
             when product associated with two categories A and category B .It always fetch from category A even though we request for category B product .
                    If you don't have much knowledge on Commerce server API then get from knowledge  from here.....
                             
  Runtime Commerce Server APIs (those that are used to retrieve info for use in a e-commerce website)
    * ProductCatalog: Getting products from a catalog along with its child products
    * CatalogContext.GetCatalogs: Getting a set of catalogs from the current context
    * CatalogContext.GetCategory: Getting the details of a category including child categories
    * CatalogContext.GetProduct: Getting the details of a product and product family including the variants
    * CatalogSearch: Searching the catalog with the basic properties specified
   
Here is the code for Fetching product based on product id and Category
/// <summary>
       /// Method:GetProduct
       /// Description: This method is used to get the Product Information based on productId and CategoryName .
       /// </summary>
       /// <returns>datatable</returns> 
       public static DataTable  GetProductByCategory(string currentCatalog, string categoryName, string productId, string language)
       {
           ProductCatalog virtualCatalog = CommerceContext.Current.CatalogSystem.GetCatalog(currentCatalog);
           Category virtualCatalogCategory = virtualCatalog.GetCategory(categoryName);
           DataSet dsProducts =  virtualCatalogCategory.GetProducts();  
           DataView dvFilteredProducts = dsProducts.Tables[0].DefaultView;
           dvFilteredProducts.RowFilter = "ProductId='" + productId + "'";
           return dvFilteredProducts.ToTable();
       }