/// <summary>
        /// Gets the system message.
        /// </summary>
        /// <param name="messageKey">The message key.</param>
        /// <returns>A system message based on the key</returns>
        public static string GetSystemMessage(string messageKey)
        {
            string indexName        = string.Format(CultureInfo.InvariantCulture, IndexNameFormat, Context.Database.Name);
            string contentStartPath = CurrentStorefront.GlobalItem.Axes.GetItem(string.Concat(StorefrontConstants.KnowItemNames.Lookups, "/", StorefrontConstants.KnowItemNames.SystemMessages)).Paths.Path;
            var    searchIndex      = ContentSearchManager.GetIndex(indexName);

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <SearchResultItem>();
                searchResults = searchResults.Where(item => item.Path.StartsWith(contentStartPath));
                searchResults = searchResults.Where(item => item.Language == SearchNavigation.CurrentLanguageName);
                searchResults = searchResults.Where(item => item.Name == messageKey);

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

                if (response.ResponseItems == null || response.TotalItemCount == 0)
                {
                    return(string.Empty);
                }

                var resultItem = response.ResponseItems.FirstOrDefault();
                if (resultItem == null)
                {
                    return(string.Empty);
                }

                var value = resultItem.Fields[StorefrontConstants.KnownFieldNames.Value];
                return(value == null ? string.Empty : value.Value);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Searches for site content items based on keyword
        /// </summary>
        /// <param name="keyword">The keyword to search for.</param>
        /// <param name="searchOptions">The paging options for this query</param>
        /// <returns>
        /// A list of child products
        /// </returns>
        public static SearchResponse SearchSiteByKeyword(string keyword, CommerceSearchOptions searchOptions)
        {
            const string IndexNameFormat = "sitecore_{0}_index";
            string       indexName       = string.Format(
                System.Globalization.CultureInfo.InvariantCulture,
                IndexNameFormat,
                Sitecore.Context.Database.Name);

            var searchIndex = ContentSearchManager.GetIndex(indexName);

            using (var context = searchIndex.CreateSearchContext())
            {
                //var rootSearchPath = Sitecore.IO.FileUtil.MakePath(Sitecore.Context.Site.ContentStartPath, "Home", '/');
                var searchResults = context.GetQueryable <SearchResultItem>();
                searchResults = searchResults.Where(item => item.Path.StartsWith(Sitecore.Context.Site.ContentStartPath));
                searchResults = searchResults.Where(item => item[StorefrontConstants.KnownIndexFields.SiteContentItem] == "1");
                searchResults = searchResults.Where(item => item.Language == CurrentLanguageName);
                searchResults = searchResults.Where(GetContentExpression(keyword));
                searchResults = searchResults.Page(searchOptions.StartPageIndex, searchOptions.NumberOfItemsToReturn);

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

                return(response);
            }
        }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Searches for catalog items based on keyword
        /// </summary>
        /// <param name="keyword">The keyword to search for.</param>
        /// <param name="catalogName">The name of the catalog containing the keyword</param>
        /// <param name="searchOptions">The paging options for this query</param>
        /// <returns>A list of child products</returns>
        public static SearchResponse SearchCatalogItemsByKeyword(string keyword, string catalogName, CommerceSearchOptions searchOptions)
        {
            Sitecore.Diagnostics.Assert.ArgumentNotNullOrEmpty(catalogName, "catalogName");
            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();
            var searchIndex   = searchManager.GetIndex(catalogName);

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <CommerceProductSearchResultItem>()
                                    .Where(item => item.Name.Equals(keyword) || item["_displayname"].Equals(keyword) || item.Content.Contains(keyword))
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Product || item.CommerceSearchItemType == CommerceSearchResultItemType.Category)
                                    .Where(item => item.CatalogName == catalogName)
                                    .Where(item => item.Language == CurrentLanguageName)
                                    .Select(p => new CommerceProductSearchResultItem()
                {
                    ItemId = p.ItemId,
                    Uri    = p.Uri
                });

                searchResults = searchManager.AddSearchOptionsToQuery <CommerceProductSearchResultItem>(searchResults, searchOptions);

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

                return(response);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets all the products under a specific category
        /// </summary>
        /// <param name="categoryId">The category name</param>
        /// <param name="searchOptions">The paging options for this query</param>
        /// <returns>A list of child products</returns>
        public static SearchResponse GetCategoryProducts(ID categoryId, CommerceSearchOptions searchOptions)
        {
            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();
            var searchIndex   = searchManager.GetIndex();

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

                searchResults = searchManager.AddSearchOptionsToQuery <CommerceProductSearchResultItem>(searchResults, searchOptions);

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

                return(response);
            }
        }
Exemplo n.º 6
0
        public static SearchResponse GetNavigationCategories(string navigationDataSource, CommerceSearchOptions searchOptions)
        {
            ID  navigationId;
            var searchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();
            var searchIndex   = searchManager.GetIndex();

            if (navigationDataSource.IsGuid())
            {
                navigationId = ID.Parse(navigationDataSource);
            }
            else
            {
                using (var context = searchIndex.CreateSearchContext())
                {
                    var query = LinqHelper.CreateQuery <Sitecore.ContentSearch.SearchTypes.SitecoreUISearchResultItem>(context, SearchStringModel.ParseDatasourceString(navigationDataSource))
                                .Select(result => result.GetItem().ID);
                    if (query != null & query.Any())
                    {
                        navigationId = query.First();
                    }
                    else
                    {
                        return(new SearchResponse());
                    }
                }
            }

            using (var context = searchIndex.CreateSearchContext())
            {
                var searchResults = context.GetQueryable <CommerceBaseCatalogSearchResultItem>()
                                    .Where(item => item.CommerceSearchItemType == CommerceSearchResultItemType.Category)
                                    .Where(item => item.Language == CurrentLanguageName)
                                    .Where(item => item.CommerceAncestorIds.Contains(navigationId))
                                    .Select(p => new CommerceBaseCatalogSearchResultItem()
                {
                    ItemId = p.ItemId,
                    Uri    = p.Uri
                });

                searchResults = searchManager.AddSearchOptionsToQuery <CommerceBaseCatalogSearchResultItem>(searchResults, searchOptions);

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

                return(response);
            }
        }
Exemplo n.º 7
0
        public SearchResults GetProducts(
            string catalogName,
            ID categoryId,
            CommerceSearchOptions searchOptions,
            string searchKeyword)
        {
            Assert.ArgumentNotNull(catalogName, nameof(catalogName));

            var commerceSearchManager = CommerceTypeLoader.CreateInstance <ICommerceSearchManager>();

            var queryable = this.GetBaseQueryable(catalogName, ProductCommerceSearchItemType);

            if (!ID.IsNullOrEmpty(categoryId))
            {
                queryable = queryable.Where(x => x.CommerceAncestorIds.Contains(categoryId));
            }

            if (!string.IsNullOrEmpty(searchKeyword))
            {
                queryable = queryable.Where(item => item.Name.Contains(searchKeyword) || item["_displayname"].Contains(searchKeyword));
            }

            queryable = commerceSearchManager.AddSearchOptionsToQuery(queryable, searchOptions);

            var results = queryable.GetResults();

            var searchResultsItems = SearchResponse.CreateFromSearchResultsItems(searchOptions, results);

            if (searchResultsItems != null)
            {
                return(new SearchResults(
                           searchResultsItems.ResponseItems,
                           searchResultsItems.TotalItemCount,
                           searchResultsItems.TotalPageCount,
                           searchOptions.StartPageIndex,
                           searchResultsItems.Facets.ToList()));
            }

            return(new SearchResults());
        }
Exemplo n.º 8
0
 public SearchResponse CreateFromSearchResultsItems <T>(
     CommerceSearchOptions searchOptions,
     SearchResults <T> sitecoreSearchResults) where T : SearchResultItem
 {
     return(SearchResponse.CreateFromSearchResultsItems(searchOptions, sitecoreSearchResults));
 }