// Method that loads the required data
        // Called only if the data doesn't already exist in the cache
        private List <UcommerceProductDto> LoadProducts(CacheSettings cs, Category category)
        {
            // Loads all products from this category from the database
            if (category == null)
            {
                return(ConvertToUcommerceProduct(new List <Product>()));
            }

            GetAllProductsRecursive(category);

            var facetsForQuerying = GetFacets();
            var filterProducts    = SearchLibrary.GetProductsFor(category, facetsForQuerying);
            var listOfProducts    = new List <Product>();

            SetCacheDependency(cs, category);

            if (!filterProducts.Any())
            {
                return(ConvertToUcommerceProduct(_products));
            }

            foreach (var product in filterProducts)
            {
                Product filterProduct = _products.FirstOrDefault(x => x.Sku == product.Sku && x.VariantSku == product.VariantSku);
                if (filterProduct != null)
                {
                    listOfProducts.Add(filterProduct);
                }
            }

            return(ConvertToUcommerceProduct(listOfProducts));
        }
Example #2
0
        private IList <ProductViewModel> MapProductsInCategories(Category category)
        {
            IList <Facet> facetsForQuerying  = System.Web.HttpContext.Current.Request.QueryString.ToFacets();
            var           productsInCategory = new List <ProductViewModel>();

            foreach (var subcategory in category.Categories)
            {
                productsInCategory.AddRange(MapProductsInCategories(subcategory));
            }

            productsInCategory.AddRange(MapProducts(SearchLibrary.GetProductsFor(category, facetsForQuerying)));

            return(productsInCategory);
        }
Example #3
0
        private IQueryable <Product> ApplyAutoSelection(Category currentCategory, string searchTerm)
        {
            IList <UCommerce.Documents.Product> products = null;
            var facetsResolver    = new FacetResolver(this.queryStringBlackList);
            var facetsForQuerying = facetsResolver.GetFacetsFromQueryString();

            if (currentCategory == null)
            {
                if (!string.IsNullOrWhiteSpace(searchTerm))
                {
                    var matchingProducts = Product.Find(p =>
                                                        p.VariantSku == null &&
                                                        p.DisplayOnSite &&
                                                        (p.Sku.Contains(searchTerm) ||
                                                         p.Name.Contains(searchTerm) ||
                                                         p.ProductDescriptions.Any(d => d.DisplayName.Contains(searchTerm) || d.ShortDescription.Contains(searchTerm) || d.LongDescription.Contains(searchTerm))));

                    return(matchingProducts.AsQueryable());
                }
                else
                {
                    var catalog = CatalogLibrary.GetAllCatalogs().FirstOrDefault();
                    if (catalog != null)
                    {
                        currentCategory = CatalogLibrary.GetRootCategories(catalog.Id).FirstOrDefault();
                        products        = SearchLibrary.GetProductsFor(currentCategory, facetsForQuerying);
                        if (currentCategory == null)
                        {
                            throw new InvalidOperationException(NO_CATEGORIES_ERROR_MESSAGE);
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException(NO_CATALOG_ERROR_MESSAGE);
                    }
                }
            }
            else
            {
                products = SearchLibrary.GetProductsFor(currentCategory, facetsForQuerying);
            }

            ICollection <Product> productsInCategory = null;

            productsInCategory = CatalogLibrary.GetProducts(currentCategory).ToList();

            if (!products.Any())
            {
                return(productsInCategory.AsQueryable());
            }

            var listOfProducts = new List <Product>();

            foreach (var product in products)
            {
                var filterProduct = productsInCategory.FirstOrDefault(x => x.Sku == product.Sku && x.VariantSku == product.VariantSku);
                if (filterProduct != null)
                {
                    listOfProducts.Add(filterProduct);
                }
            }

            return(listOfProducts.AsQueryable());
        }