示例#1
0
        public SearchResponse GetCategoryProducts(Item categoryItem, SearchOptions searchOptions)
        {
            if (categoryItem.IsDerived(Templates.Commerce.DynamicCategory.Id))
            {
                return(FindCatalogItems(categoryItem, searchOptions));
            }

            var searchIndex = CommerceSearchManager.GetIndex();

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <CommerceProductSearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product)
                                    .Where(item => item.Language == Context.Language.Name)
                                    .Where(item => item.CommerceAncestorIds.Contains(categoryItem.ID))
                                    .Select(p => new CommerceProductSearchResultItem
                {
                    ItemId = p.ItemId,
                    Uri    = p.Uri
                });

                var commerceSearchOptions = searchOptions.ToCommerceSearchOptions();
                searchResults = CommerceSearchManager.AddSearchOptionsToQuery(searchResults, commerceSearchOptions);

                var results  = searchResults.GetResults();
                var response = SearchResponse.CreateFromSearchResultsItems(commerceSearchOptions, results);

                searchOptions = commerceSearchOptions.ToSearchOptions();

                return(response);
            }
        }
示例#2
0
        public Category GetCategory(Item item)
        {
            var category = new Category(item)
            {
                RequiredFacets = CommerceSearchManager.GetFacetFieldsForItem(item).Select(f => f.ToQueryFacet()).ToList(),
                SortFields     = CommerceSearchManager.GetSortFieldsForItem(item).Select(f => f.ToQuerySortField()).ToList(),
                ItemsPerPage   = CommerceSearchManager.GetItemsPerPageForItem(item)
            };

            return(category);
        }
示例#3
0
        public CategorySearchResults GetCategoryChildCategories(Item category)
        {
            if (category == null)
            {
                throw new ArgumentNullException(nameof(category));
            }

            var childCategoryList = new List <Item>();

            var searchIndex = CommerceSearchManager.GetIndex();

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <CommerceBaseCatalogSearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Category)
                                    .Where(item => item.Language == Context.Language.Name)
                                    .Where(item => item.ItemId == category.ID)
                                    .Select(p => p);

                var list = searchResults.ToList();
                if (!list.Any())
                {
                    return(new CategorySearchResults(childCategoryList, childCategoryList.Count, 1, 1, new List <FacetCategory>()));
                }
                if (!list[0].Fields.ContainsKey(Constants.CommerceIndex.Fields.ChildCategoriesSequence))
                {
                    return(new CategorySearchResults(childCategoryList, childCategoryList.Count, 1, 1, new List <FacetCategory>()));
                }

                var childCategoryDelimitedString = list[0][Constants.CommerceIndex.Fields.ChildCategoriesSequence];

                var categoryIdArray = childCategoryDelimitedString.Split('|');

                var categoryItems = categoryIdArray.Select(childCategoryId => Context.Database.GetItem(ID.Parse(childCategoryId))).Where(childCategoryItem => childCategoryItem != null);
                childCategoryList.AddRange(categoryItems);
            }

            return(new CategorySearchResults(childCategoryList, childCategoryList.Count, 1, 1, new List <FacetCategory>()));
        }
示例#4
0
        public SearchResponse FindCatalogItems(Item queryItem, SearchOptions searchOptions)
        {
            Assert.ArgumentNotNull(queryItem, nameof(queryItem));
            Assert.ArgumentNotNull(searchOptions, nameof(searchOptions));

            var defaultBucketQuery     = queryItem[Templates.Commerce.SearchSettings.Fields.DefaultBucketQuery];
            var persistentBucketFilter = CleanLanguageFromFilter(queryItem[Templates.Commerce.SearchSettings.Fields.PersistentBucketFilter]);

            var searchIndex = CommerceSearchManager.GetIndex();

            var defaultQuery    = defaultBucketQuery.Replace("&", ";");
            var persistentQuery = persistentBucketFilter.Replace("&", ";");
            var combinedQuery   = CombineQueries(persistentQuery, defaultQuery);

            try
            {
                var searchStringModel = SearchStringModel.ParseDatasourceString(combinedQuery);
                using (var context = searchIndex.CreateSearchContext(SearchSecurityOptions.EnableSecurityCheck))
                {
                    var query = LinqHelper.CreateQuery <SitecoreUISearchResultItem>(context, searchStringModel).Where(item => item.Language == Context.Language.Name);

                    var commerceSearchOptions = searchOptions.ToCommerceSearchOptions();
                    query = CommerceSearchManager.AddSearchOptionsToQuery(query, commerceSearchOptions);

                    var results  = query.GetResults();
                    var response = SearchResponse.CreateFromUISearchResultsItems(commerceSearchOptions, results);
                    searchOptions = commerceSearchOptions.ToSearchOptions();

                    return(response);
                }
            }
            catch (Exception e)
            {
                Log.Error("Could not find catalog items. Bucket query failed", e, this);
                return(null);
            }
        }
        private void GetProductStockStatusFromIndex(IEnumerable <IInventoryProduct> viewModelList)
        {
            var products = viewModelList as IInventoryProduct[] ?? viewModelList.ToArray();

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

            var searchIndex = CommerceSearchManager.GetIndex();

            using (var context = searchIndex.CreateSearchContext())
            {
                var predicate = PredicateBuilder.Create <InventorySearchResultItem>(item => item[Constants.CommerceIndex.Fields.InStockLocations].Contains("Default"));
                predicate = predicate.Or(item => item[Constants.CommerceIndex.Fields.OutOfStockLocations].Contains("Default"));
                predicate = predicate.Or(item => item[Constants.CommerceIndex.Fields.OrderableLocations].Contains("Default"));
                predicate = predicate.Or(item => item[Constants.CommerceIndex.Fields.PreOrderable].Contains("0"));

                var searchResults = context.GetQueryable <InventorySearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product)
                                    .Where(item => item.Language == Context.Language.Name)
                                    .Where(BuildProductIdListPredicate(products))
                                    .Where(predicate)
                                    .Select(x => new { x.OutOfStockLocations, x.OrderableLocations, x.PreOrderable, x.InStockLocations, x.Fields, x.Name });

                var results = searchResults.GetResults();
                if (results.TotalSearchResults == 0)
                {
                    return;
                }

                foreach (var result in results)
                {
                    var resultDocument = result.Document;
                    if (resultDocument == null)
                    {
                        continue;
                    }

                    StockStatus status;

                    var isInStock = resultDocument.Fields.ContainsKey(Constants.CommerceIndex.Fields.InStockLocations) &&
                                    resultDocument.Fields[Constants.CommerceIndex.Fields.InStockLocations] != null;
                    if (isInStock)
                    {
                        status = StockStatus.InStock;
                    }
                    else
                    {
                        var isPreOrderable = resultDocument.Fields.ContainsKey(Constants.CommerceIndex.Fields.PreOrderable) &&
                                             result.Document.PreOrderable != null &&
                                             (result.Document.PreOrderable.Equals("1", StringComparison.OrdinalIgnoreCase) ||
                                              result.Document.PreOrderable.Equals("true", StringComparison.OrdinalIgnoreCase));
                        if (isPreOrderable)
                        {
                            status = StockStatus.PreOrderable;
                        }
                        else
                        {
                            var isOutOfStock = resultDocument.Fields.ContainsKey(Constants.CommerceIndex.Fields.OutOfStockLocations) &&
                                               result.Document.OutOfStockLocations != null;
                            var isBackOrderable = resultDocument.Fields.ContainsKey(Constants.CommerceIndex.Fields.OrderableLocations) &&
                                                  result.Document.OrderableLocations != null;
                            if (isOutOfStock && isBackOrderable)
                            {
                                status = StockStatus.BackOrderable;
                            }
                            else
                            {
                                status = isOutOfStock ? StockStatus.OutOfStock : null;
                            }
                        }
                    }

                    var foundModel = products.FirstOrDefault(x => x.ProductId == result.Document.Name);
                    if (foundModel == null)
                    {
                        continue;
                    }

                    foundModel.StockStatus     = status;
                    foundModel.StockStatusName = GetStockStatusName(foundModel.StockStatus);
                }
            }
        }